TypeScript 類
TypeScript 為 JavaScript 類添加了型別和可見性修飾符。
在此處 瞭解更多關於 JavaScript 類。
成員:型別
類的成員(屬性和方法)使用型別註解進行型別化,類似於變數。
成員:可見性
類成員還可以新增特殊的修飾符,這些修飾符會影響可見性。
TypeScript 中有三種主要的可見性修飾符。
public
- (預設)允許從任何地方訪問類成員private
- 僅允許從類內部訪問類成員protected
- 允許類成員本身以及繼承它的任何類訪問,這在下面的繼承部分中介紹
示例
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()); // person.name 由於是私有的,因此無法從類外部訪問
自己動手試一試 »
類中的
this
關鍵字通常指向類的例項。在此處 瞭解更多關於 this
的資訊。引數屬性
TypeScript 提供了一種在建構函式中定義類成員的便捷方法,方法是為引數新增可見性修飾符。
示例
class Person {
// name 是一個私有成員變數
public constructor(private name: string) {}
public getName(): string {
return this.name;
}
}
const person = new Person("Jane");
console.log(person.getName());
自己動手試一試 »
只讀
與陣列類似,readonly
關鍵字可以防止類成員被更改。
示例
class Person {
private readonly name: string;
public constructor(name: string) {
// name 在此初始定義後不能被更改,該定義必須在宣告處或建構函式中完成。
this.name = name;
}
public getName(): string {
return this.name;
}
}
const person = new Person("Jane");
console.log(person.getName());
自己動手試一試 »
繼承:實現
介面(在此處 介紹)可用於透過 implements
關鍵字定義類必須遵循的型別。
示例
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;
}
}
自己動手試一試 »
一個類可以透過
implements
關鍵字後跟逗號分隔的列表來實多個介面,如下所示:class Rectangle implements Shape, Colored {
繼承:擴充套件
類可以透過 extends
關鍵字進行擴充套件。一個類只能擴充套件另一個類。
示例
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 繼承
}
自己動手試一試 »
重寫
當一個類擴充套件另一個類時,它可以用同名成員替換父類的成員。
較新版本的 TypeScript 允許使用 override
關鍵字顯式標記此操作。
示例
interface Shape {
getArea: () => number;
}
class Rectangle implements Shape {
// 使用 protected 修飾符可以允許擴充套件此類的類(如 Square)訪問這些成員
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);
}
// 此 toString 方法覆蓋了 Rectangle 的 toString 方法
public override toString(): string {
return `Square[width=${this.width}]`;
}
}
自己動手試一試 »
預設情況下,重寫方法時
override
關鍵字是可選的,它僅有助於防止意外重寫不存在的方法。使用 noImplicitOverride
設定來強制在重寫時使用它。抽象類
類可以以允許它們用作其他類的基類而無需實現所有成員的方式編寫。這是透過使用 abstract
關鍵字完成的。未實現的成員也使用 abstract
關鍵字。
示例
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;
}
}
自己動手試一試 »
抽象類不能直接例項化,因為它們沒有實現所有成員。