選單
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

TypeScript 類


TypeScript 為 JavaScript 類添加了型別和可見性修飾符。

在此處 瞭解更多關於 JavaScript 類


成員:型別

類的成員(屬性和方法)使用型別註解進行型別化,類似於變數。

示例

class Person {
  name: string;
}

const person = new Person();
person.name = "Jane";
自己動手試一試 »

成員:可見性

類成員還可以新增特殊的修飾符,這些修飾符會影響可見性。

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());
自己動手試一試 »

w3schools CERTIFIED . 2022

獲得認證!

完成 TypeScript 模組,做練習,參加考試,成為 W3Schools 認證!!

$45 報名

繼承:實現

介面(在此處 介紹)可用於透過 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;
  }
}
自己動手試一試 »
抽象類不能直接例項化,因為它們沒有實現所有成員。

TypeScript 練習

透過練習來測試自己

練習

指定 Person.name 只能在類內部訪問,而 Person.getName() 方法可以在任何地方訪問

class Person {
  name: string;

 public constructor(name: string) {
  this.name = name;
 }

  getName(): string {
  return this.name;
 }
}

開始練習


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援