TypeScript學習筆記(三):類

來源:互聯網
上載者:User

標籤:

類

在TypeScript中,類似於C#的結構,即一個檔案中可以存在多個類,且檔案名稱可以任意取,我們先看一個簡單的類的樣本。

 1 class Person { 2     private name: string; 3     private age: number; 4  5     public constructor(name:string, age:number) { 6         this.name = name; 7         this.age = age; 8     } 9 10     public talk() {11         alert("Hi, my name is " + this.name + ", i am " + this.age.toString() + "!");12     }13 }14 15 function run() {16     var p: Person = new Person("Li Lei", 18);17     p.talk();18 }19 20 run();

TypeScript中的類需要注意下面幾個點:

  • class前不用加存取控制符,所有類都是全域公開可訪問的;
  • 如果類的屬性和方法不加存取控制符則預設為public;
  • 1.3開始支援protected存取權限,1.3之前僅支援public及private;
  • 建構函式不是類名而是constructor;
  • 訪問類的成員變數及函數都需要加this關鍵字;
類的繼承

在TypeScript中,如何?類的繼承呢,請看如下樣本:

 1 class Person 2 { 3     private name: string; 4     private age: number; 5  6     constructor(name: string, age: number) 7     { 8         this.name = name; 9         this.age = age;10     }11 12     talk()13     {14         alert("Hi, my name is " + this.name + ", i am " + this.age.toString() + "!");15     }16 }17 18 class Student extends Person19 {20     private school: string;21 22     constructor(name: string, age: number, school: string)23     {24         super(name, age);25         this.school = school;26     }27 28     talk()29     {30         super.talk();31         alert("And I`m in " + this.school + "!");32     }33 }34 35 function run()36 {37     var p: Person = new Student("Li Lei", 18, "English School");38     p.talk();39 }40 41 run();

類的繼承需要注意以下幾點:

  • 繼承使用extends關鍵字;
  • 調用父級使用super關鍵字;
  • 重寫父級方法不需要加override關鍵字,直接定義一樣的方法即可;
  • 父級變數可以調用到子級的方法,符合多態的概念;
getter/setter封裝

在TypeScript中我們可以對一個成員變數進行getter和setter的封裝,如下:

 1 class Person 2 { 3     private _name:string; 4  5     constructor(name: string) 6     { 7         this._name = name; 8     } 9 10     set name(value:string) {this._name = value;}11     get name() {return this._name;}12 13     talk()14     {15         alert("Hi, my name is " + this.name + "!");16     }17 }18 19 function run()20 {21     var p:Person = new Person(null);22     p.name = "Li Lie";23     p.talk();24 }25 26 run();
待用資料和方法

在TypeScript中,是可以使用Static來定義靜態變數及靜態函數的,我們以單例類為例來看看:

 1 class Person 2 { 3     private static _instance:Person; 4  5     static getInstance():Person 6     { 7         if(this._instance == null) 8         { 9             this._instance = new Person();10         }11         return this._instance;12     }13 14     talk()15     {16         alert("Hi, my name is Li Lie!");17     }18 }19 20 function run()21 {22     Person.getInstance().talk();23 }24 25 run();

 

TypeScript學習筆記(三):類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.