類
成員:類型
類的成員(屬性和方法)使用類型注解進(jìn)行類型化,類似于變量。
實(shí)例
class Person { name: string; } const person = new Person(); person.name = "Jane";
成員:可見(jiàn)性
類成員還可以被賦予特殊的修飾符,這些修飾符會(huì)影響其可見(jiàn)性。
TypeScript 中主要有三個(gè)可見(jiàn)性修飾符。
public
- (默認(rèn))允許從任何地方訪問(wèn)類成員private
- 僅允許從類內(nèi)部訪問(wèn)類成員protected
- 允許從類本身和任何繼承它的類中訪問(wèn)類成員,這在下面的繼承部分中有介紹
實(shí)例
class Person { private name: string; public constructor(name: string) { this.name = name; } public getName(): string { return this.name; } } const person = new Person("Jane"); console.log(person.getName()); // 由于 name 是私有的,因此無(wú)法從類外部訪問(wèn) person.name
類中的 this
關(guān)鍵字通常指的是類的實(shí)例。在此處閱讀更多有關(guān) this
的信息。
參數(shù)屬性
TypeScript 提供了一種在構(gòu)造函數(shù)中定義類成員的便捷方式,方法是在參數(shù)中添加可見(jiàn)性修飾符。
實(shí)例
class Person { // name 是一個(gè)私有成員變量 public constructor(private name: string) {} public getName(): string { return this.name; } } const person = new Person("Jane"); console.log(person.getName());
只讀
與數(shù)組類似,readonly
關(guān)鍵字可以防止類成員被更改。
實(shí)例
class Person { private readonly name: string; public constructor(name: string) { // 名稱在初始定義后無(wú)法更改,這必須在聲明時(shí)或在構(gòu)造函數(shù)中進(jìn)行。 this.name = name; } public getName(): string { return this.name; } } const person = new Person("Jane"); console.log(person.getName());
繼承:實(shí)現(xiàn)
接口(此處介紹)可用于通過(guò) implements
關(guān)鍵字定義類必須遵循的類型。
實(shí)例
interface Shape { getArea: () => number; } class Rectangle implements Shape { public constructor(protected readonly width: number, protected readonly height: number) {} public getArea(): number { return this.width * this.height; } }
一個(gè)類可以通過(guò)在 implements
后列出每一個(gè)接口,并用逗號(hào)分隔,來(lái)實(shí)現(xiàn)多個(gè)接口,如下所示:
class Rectangle implements Shape, Colored {
繼承:擴(kuò)展
類可以通過(guò) extends
關(guān)鍵字相互繼承。一個(gè)類只能擴(kuò)展一個(gè)其他類。
實(shí)例
interface Shape { getArea: () => number; } class Rectangle implements Shape { public constructor(protected readonly width: number, protected readonly height: number) {} public getArea(): number { return this.width * this.height; } } class Square extends Rectangle { public constructor(width: number) { super(width, width); } // getArea 從 Rectangle 繼承 }
重寫
當(dāng)一個(gè)類擴(kuò)展另一個(gè)類時(shí),它可以替換父類中具有相同名稱的成員。
較新版本的 TypeScript 允許使用 override
關(guān)鍵字顯式標(biāo)記這一點(diǎn)。
實(shí)例
interface Shape { getArea: () => number; } class Rectangle implements Shape { // 使用 protected 修飾符的這些成員允許從繼承自此類的類(如 Square)進(jìn)行訪問(wèn) public constructor(protected readonly width: number, protected readonly height: number) {} public getArea(): number { return this.width * this.height; } public toString(): string { return `Rectangle[width=${this.width}, height=${this.height}]`; } } class Square extends Rectangle { public constructor(width: number) { super(width, width); } // 這個(gè) toString 替換了 Rectangle 的 toString public override toString(): string { return `Square[width=${this.width}]`; } }
默認(rèn)情況下,當(dāng)重寫方法時(shí),override
關(guān)鍵字是可選的,它僅有助于防止意外重寫不存在的方法。使用 noImplicitOverride
設(shè)置來(lái)強(qiáng)制在重寫時(shí)使用它。
抽象類
類的編寫方式可以允許它們用作其他類的基類,而不必實(shí)現(xiàn)所有成員。這是通過(guò)使用 abstract
關(guān)鍵字來(lái)完成的。未實(shí)現(xiàn)的成員也使用 abstract
關(guān)鍵字。
實(shí)例
abstract class Polygon { public abstract getArea(): number; public toString(): string { return `Polygon[area=${this.getArea()}]`; } } class Rectangle extends Polygon { public constructor(protected readonly width: number, protected readonly height: number) { super(); } public getArea(): number { return this.width * this.height; } }
抽象類不能直接實(shí)例化,因?yàn)樗鼈儧](méi)有實(shí)現(xiàn)所有成員。