自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

TS 類這十個知識點,你都掌握了么?

開發(fā) 前端
在面向對象語言中,類是一種面向對象計算機編程語言的構造,是創(chuàng)建對象的藍圖,描述了所創(chuàng)建的對象共同的屬性和方法。本文阿寶哥將跟大家一起學習一下 TS 類涉及的十個知識點。

[[429439]]

在面向對象語言中,類是一種面向對象計算機編程語言的構造,是創(chuàng)建對象的藍圖,描述了所創(chuàng)建的對象共同的屬性和方法。本文阿寶哥將跟大家一起學習一下 TS 類涉及的十個知識點。

一、類的屬性與方法

1.1 類的成員屬性和靜態(tài)屬性

在 TypeScript 中,我們可以通過 class 關鍵字來定義一個類:

  1. class Person { 
  2.   name: string; // 成員屬性 
  3.    
  4.   constructor(name: string) { // 類的構造函數(shù) 
  5.     this.name = name
  6.   } 

在以上代碼中,我們使用 class 關鍵字定義了一個 Person 類,該類含有一個名為 name 的成員屬性。其實 TypeScript 中的類是一個語法糖(所謂的語法糖就是在之前的某個語法的基礎上改變了一種寫法,實現(xiàn)的功能相同,但是寫法不同了,主要是為了讓開發(fā)人員在使用過程中更方便易懂。),若設置編譯目標為 ES5 將會產生以下代碼:

  1. "use strict"
  2. var Person = /** @class */ (function () { 
  3.     function Person(name) { 
  4.         this.name = name
  5.     } 
  6.     return Person; 
  7. }()); 

類除了可以定義成員屬性外,還可以通過 static 關鍵字定義靜態(tài)屬性:

  1. class Person { 
  2.   static cid: string = "exe"
  3.   name: string; // 成員屬性 
  4.    
  5.   constructor(name: string) { // 類的構造函數(shù) 
  6.     this.name = name
  7.   } 

那么成員屬性與靜態(tài)屬性有什么區(qū)別呢?在回答這個問題之前,我們先來看一下編譯生成的 ES5 代碼:

  1. "use strict"
  2. var Person = /** @class */ (function () { 
  3.     function Person(name) { 
  4.       this.name = name
  5.     } 
  6.     Person.cid = "exe"
  7.     return Person; 
  8. }()); 

觀察以上代碼可知,成員屬性是定義在類的實例上,而靜態(tài)屬性是定義在構造函數(shù)上。

1.2 類的成員方法和靜態(tài)方法

在 TS 類中,我們不僅可以定義成員屬性和靜態(tài)屬性,還可以定義成員方法和靜態(tài)方法,具體如下所示:

  1. class Person { 
  2.   static cid: string = "exe"
  3.   name: string; // 成員屬性 
  4.    
  5.   static printCid() { // 定義靜態(tài)方法 
  6.     console.log(Person.cid);   
  7.   } 
  8.  
  9.   constructor(name: string) { // 類的構造函數(shù) 
  10.     this.name = name
  11.   } 
  12.  
  13.   say(words: string) :void { // 定義成員方法 
  14.     console.log(`${this.name} says:${words}`);   
  15.   } 

那么成員方法與靜態(tài)方法有什么區(qū)別呢?同樣,在回答這個問題之前,我們先來看一下編譯生成的 ES5 代碼:

  1. "use strict"
  2. var Person = /** @class */ (function () { 
  3.     function Person(name) { 
  4.         this.name = name
  5.     } 
  6.     Person.printCid = function () { 
  7.         console.log(Person.cid); 
  8.     }; 
  9.     Person.prototype.say = function (words) { 
  10.         console.log(this.name + " says\uFF1A" + words); 
  11.     }; 
  12.     Person.cid = "exe"
  13.     return Person; 
  14. }()); 

由以上代碼可知,成員方法會被添加到構造函數(shù)的原型對象上,而靜態(tài)方法會被添加到構造函數(shù)上。

1.3 類成員方法重載

函數(shù)重載或方法重載是使用相同名稱和不同參數(shù)數(shù)量或類型創(chuàng)建多個方法的一種能力。 在定義類的成員方法時,我們也可以對成員方法進行重載:

  1. class Person { 
  2.   constructor(public name: string) {} 
  3.  
  4.   say(): void;  
  5.   say(words: string): void; 
  6.   say(words?: string) :void { // 方法重載 
  7.     if(typeof words === "string") { 
  8.         console.log(`${this.name} says:${words}`);   
  9.     } else { 
  10.         console.log(`${this.name} says:Nothing`);   
  11.     } 
  12.   } 
  13.  
  14. let p1 = new Person("Semlinker"); 
  15. p1.say(); 
  16. p1.say("Hello TS"); 

如果想進一步了解函數(shù)重載的話,可以繼續(xù)閱讀 是時候表演真正的技術了 - TS 分身之術 這一篇文章。

二、訪問器

在 TypeScript 中,我們可以通過 getter 和 setter 方法來實現(xiàn)數(shù)據(jù)的封裝和有效性校驗,防止出現(xiàn)異常數(shù)據(jù)。

  1. let passcode = "Hello TypeScript"
  2.  
  3. class Employee { 
  4.   private _fullName: string = ""
  5.  
  6.   get fullName(): string { 
  7.     return this._fullName; 
  8.   } 
  9.  
  10.   set fullName(newName: string) { 
  11.     if (passcode && passcode == "Hello TypeScript") { 
  12.       this._fullName = newName; 
  13.     } else { 
  14.       console.log("Error: Unauthorized update of employee!"); 
  15.     } 
  16.   } 
  17.  
  18. let employee = new Employee(); 
  19. employee.fullName = "Semlinker"

在以上代碼中,對于私有的 _fullName 屬性,我們通過對外提供 getter 和 setter 來控制該屬性的訪問和修改。

三、類的繼承

繼承(Inheritance)是一種聯(lián)結類與類的層次模型。指的是一個類(稱為子類、子接口)繼承另外的一個類(稱為父類、父接口)的功能,并可以增加它自己的新功能的能力,繼承是類與類或者接口與接口之間最常見的關系。通過類的繼承,我們可以實現(xiàn)代碼的復用。

繼承是一種 is-a 關系:

在 TypeScript 中,我們可以通過 extends 關鍵字來實現(xiàn)類的繼承:

3.1 父類

  1. class Person { 
  2.   constructor(public name: string) {} 
  3.  
  4.   public say(words: string) :void { 
  5.     console.log(`${this.name} says:${words}`);   
  6.   } 

3.2 子類

  1. class Developer extends Person { 
  2.   constructor(name: string) { 
  3.     super(name); 
  4.     this.say("Learn TypeScript"
  5.   } 
  6.  
  7. const p2 = new Developer("semlinker");  
  8. // 輸出: "semlinker says:Learn TypeScript"  

因為 Developer 類繼承了 Person 類,所以我們可以在 Developer 類的構造函數(shù)中調用 say 方法。需要注意的是,在 TypeScript 中使用 extends 時,只能繼承單個類:

  1. class Programmer {} 
  2.  
  3. // Classes can only extend a single class.(1174) 
  4. class Developer extends Person, Programmer { 
  5.   constructor(name: string) { 
  6.     super(name); 
  7.     this.say("Learn TypeScript"
  8.   } 

雖然在 TypeScript 中只允許單繼承,但卻允許我們實現(xiàn)多個接口。具體的使用示例如下所示:

  1. interface CanSay { 
  2.    say(words: string) :void  
  3.  
  4. interface CanWalk { 
  5.   walk(): void; 
  6.  
  7. class Person implements CanSay, CanWalk { 
  8.   constructor(public name: string) {} 
  9.  
  10.   public say(words: string) :void { 
  11.     console.log(`${this.name} says:${words}`);   
  12.   } 
  13.  
  14.   public walk(): void { 
  15.     console.log(`${this.name} walk with feet`); 
  16.   } 

此外,除了可以繼承具體的實現(xiàn)類之外,在實現(xiàn)繼承時,我們還可以繼承抽象類。

四、抽象類

使用 abstract 關鍵字聲明的類,我們稱之為抽象類。抽象類不能被實例化,因為它里面包含一個或多個抽象方法。 所謂的抽象方法,是指不包含具體實現(xiàn)的方法:

  1. abstract class Person { 
  2.   constructor(public name: string){} 
  3.  
  4.   abstract say(words: string) :void; 
  5.  
  6. // Cannot create an instance of an abstract class.(2511) 
  7. const lolo = new Person(); // Error 

抽象類不能被直接實例化,我們只能實例化實現(xiàn)了所有抽象方法的子類。具體如下所示:

  1. class Developer extends Person { 
  2.   constructor(name: string) { 
  3.     super(name); 
  4.   } 
  5.    
  6.   say(words: string): void { 
  7.     console.log(`${this.name} says ${words}`); 
  8.   } 
  9.  
  10. const lolo = new Developer("lolo"); 
  11. lolo.say("I love ts!"); // 輸出:lolo says I love ts! 

五、類訪問修飾

符在 TS 類型中,我們可以使用 public、protected 或 private 來描述該類屬性和方法的可見性。

5.1 public

public 修飾的屬性或者方法是公有的,可以在任何地方被訪問到,默認所有的屬性或者方法都是 public:

  1. class Person { 
  2.   constructor(public name: string) {} 
  3.  
  4.   public say(words: string) :void { 
  5.     console.log(`${this.name} says:${words}`);   
  6.   } 

5.2 protected

protected 修飾的屬性或者方法是受保護的,它和 private 類似,不同的地方是 protected 成員在派生類中仍然可以訪問。

  1. class Person { 
  2.   constructor(public name: string) {} 
  3.  
  4.   public say(words: string) :void { 
  5.     console.log(`${this.name} says:${words}`);   
  6.   } 
  7.  
  8.   protected getClassName() { 
  9.     return "Person"
  10.   } 
  11.  
  12. const p1 = new Person("lolo"); 
  13. p1.say("Learn TypeScript"); // Ok 
  14. // Property 'getClassName' is protected and only accessible within class 'Person' and its subclasses. 
  15. p1.getClassName() // Error 

由以上錯誤信息可知,使用 protected 修飾符修飾的方法,只能在當前類或它的子類中使用。

  1. class Developer extends Person { 
  2.   constructor(name: string) { 
  3.     super(name); 
  4.     console.log(`Base Class:${this.getClassName()}`); 
  5.   } 
  6.  
  7. const p2 = new Developer("semlinker"); // 輸出:"Base Class:Person"  

5.3 private

private 修飾的屬性或者方法是私有的,只能在類的內部進行訪問。

  1. class Person { 
  2.   constructor(private id: number, public name: string) {} 
  3.  
  4. const p1 = new Person(28, "lolo"); 
  5. // Property 'id' is private and only accessible within class 'Person'.(2341) 
  6. p1.id // Error 
  7. p1.name // OK 

由以上錯誤信息可知,使用 private 修飾符修飾的屬性,只能在當前類內部訪問。但真的是這樣么?其實這只是 TS 類型檢查器給我們的提示,在運行時我們還是可以訪問 Person 實例的 id 屬性。不相信的話,我們來看一下編譯生成的 ES5 代碼:

  1. "use strict"
  2. var Person = /** @class */ (function () { 
  3.     function Person(id, name) { 
  4.       this.id = id; 
  5.       this.name = name
  6.     } 
  7.     return Person; 
  8. }()); 
  9. var p1 = new Person(28, "lolo"); 

5.4 私有字段

針對上面的問題,TypeScript 團隊在 3.8 版本就開始支持 ECMAScript 私有字段,使用方式如下:

  1. class Person { 
  2.   #name: string; 
  3.  
  4.   constructor(name: string) { 
  5.     this.#name = name
  6.   } 
  7.  
  8. let semlinker = new Person("semlinker"); 
  9. // Property '#name' is not accessible outside class 'Person' because it has a private identifier. 
  10. semlinker.#name // Error 

那么 ECMAScript 私有字段 跟 private 修飾符相比,有什么特別之處么?這里我們來看一下編譯生成的 ES2015 代碼:

  1. "use strict"
  2. var __classPrivateFieldSet = // 省略相關代碼 
  3. var _Person_name; 
  4. class Person { 
  5.   constructor(name) { 
  6.     _Person_name.set(this, void 0); 
  7.     __classPrivateFieldSet(this, _Person_name, name"f"); 
  8.   } 
  9.  
  10. _Person_name = new WeakMap(); 
  11. let semlinker = new Person("Semlinker"); 

觀察以上的結果可知,在處理私有字段時使用到了 ES2015 新增的 WeakMap 數(shù)據(jù)類型,如果你對 WeakMap 還不了解的話,可以閱讀 你不知道的 WeakMap 這篇文章。下面我們來總結一下,私有字段與常規(guī)屬性(甚至使用 private 修飾符聲明的屬性)不同之處:

  • 私有字段以 # 字符開頭,有時我們稱之為私有名稱;
  • 每個私有字段名稱都唯一地限定于其包含的類;
  • 不能在私有字段上使用 TypeScript 可訪問性修飾符(如 public 或 private);
  • 私有字段不能在包含的類之外訪問,甚至不能被檢測到。

六、類表達式

TypeScript 1.6 添加了對 ES6 類表達式的支持。類表達式是用來定義類的一種語法。和函數(shù)表達式相同的一點是,類表達式可以是命名也可以是匿名的。如果是命名類表達式,這個名字只能在類體內部才能訪問到。

類表達式的語法如下所示([] 方括號表示是可選的):

  1. const MyClass = class [className] [extends] { 
  2.   // class body 
  3. }; 

基于類表達式的語法,我們可以定義一個 Point 類:

  1. let Point = class { 
  2.   constructor(public x: number, public y: number) {} 
  3.   public length() { 
  4.     return Math.sqrt(this.x * this.x + this.y * this.y); 
  5.   } 
  6.  
  7. let p = new Point(3, 4); 
  8. console.log(p.length()); // 輸出:5 

需要注意在使用類表達式定義類的時候,我們也可以使用 extends 關鍵字。篇幅有限,這里就不展開介紹了,感興趣的小伙伴可以自行測試一下。

七、泛型類

在類中使用泛型也很簡單,我們只需要在類名后面,使用

  1. class Person<T> { 
  2.   constructor( 
  3.     public cid: T,  
  4.     public name: string 
  5.   ) {}    
  6.  
  7. let p1 = new Person<number>(28, "Lolo"); 
  8. let p2 = new Person<string>("exe""Semlinker"); 

接下來我們以實例化 p1 為例,來分析一下其處理過程:

  • 在實例化 Person 對象時,我們傳入 number 類型和相應的構造參數(shù);
  • 之后在 Person 類中,類型變量 T 的值變成 number 類型;
  • 最后構造函數(shù) cid 的參數(shù)類型也會變成 number 類型。

相信看到這里一些讀者會有疑問,我們什么時候需要使用泛型呢?通常在決定是否使用泛型時,我們有以下兩個參考標準:

  • 當你的函數(shù)、接口或類將處理多種數(shù)據(jù)類型時;
  • 當函數(shù)、接口或類在多個地方使用該數(shù)據(jù)類型時。

八、構造簽名

在 TypeScript 接口中,你可以使用 new 關鍵字來描述一個構造函數(shù):

  1. interface Point { 
  2.   new (x: number, y: number): Point; 

以上接口中的 new (x: number, y: number) 我們稱之為構造簽名,其語法如下:

  • ConstructSignature:new TypeParametersopt ( ParameterListopt ) TypeAnnotationopt

在上述的構造簽名中,TypeParametersopt 、ParameterListopt 和 TypeAnnotationopt 分別表示:可選的類型參數(shù)、可選的參數(shù)列表和可選的類型注解。那么了解構造簽名有什么用呢?這里我們先來看個例子:

  1. interface Point { 
  2.   new (x: number, y: number): Point; 
  3.   x: number; 
  4.   y: number; 
  5.  
  6. class Point2D implements Point { 
  7.   readonly x: number; 
  8.   readonly y: number; 
  9.  
  10.   constructor(x: number, y: number) { 
  11.     this.x = x; 
  12.     this.y = y; 
  13.   } 
  14.  
  15. const point: Point = new Point2D(1, 2); // Error 

對于以上的代碼,TypeScript 編譯器(v4.4.3)會提示以下錯誤信息:

  1. Type 'Point2D' is not assignable to type 'Point'
  2. Type 'Point2D' provides no match for the signature 'new (x: number, y: number): Point'

要解決這個問題,我們就需要把對前面定義的 Point 接口進行分離:

  1. interface Point { 
  2.   x: number; 
  3.   y: number; 
  4.  
  5. interface PointConstructor { 
  6.   new (x: number, y: number): Point; 

完成接口拆分之后,除了前面已經定義的 Point2D 類之外,我們又定義了一個 newPoint 工廠函數(shù),該函數(shù)用于根據(jù)傳入的 PointConstructor 類型的構造函數(shù),來創(chuàng)建對應的 Point 對象。

  1. class Point2D implements Point { 
  2.   readonly x: number; 
  3.   readonly y: number; 
  4.  
  5.   constructor(x: number, y: number) { 
  6.     this.x = x; 
  7.     this.y = y; 
  8.   } 
  9.  
  10. function newPoint( // 工廠方法 
  11.   pointConstructor: PointConstructor, 
  12.   x: number, 
  13.   y: number 
  14. ): Point { 
  15.   return new pointConstructor(x, y); 
  16.  
  17. const point: Point = newPoint(Point2D, 1, 2); 

九、抽象構造簽名

在 TypeScript 4.2 版本中引入了抽象構造簽名,用于解決以下的問題:

  1. type ConstructorFunction = new (...args: any[]) => any
  2.  
  3. abstract class Utilities {} 
  4.  
  5. // Type 'typeof Utilities' is not assignable to type 'ConstructorFunction'
  6. // Cannot assign an abstract constructor type to a non-abstract constructor type. 
  7. let UtilityClass: ConstructorFunction = Utilities; // Error. 

由以上的錯誤信息可知,我們不能把抽象構造器類型分配給非抽象的構造器類型。針對這個問題,我們需要使用 abstract 修飾符:

  1. declare type ConstructorFunction = abstract new (...args: any[]) => any

需要注意的是,對于抽象構造器類型,我們也可以傳入具體的實現(xiàn)類:

  1. declare type ConstructorFunction = abstract new (...args: any[]) => any
  2.  
  3. abstract class Utilities {} 
  4. class UtilitiesConcrete extends Utilities {} 
  5.  
  6. let UtilityClass: ConstructorFunction = Utilities; // Ok 
  7. let UtilityClass1: ConstructorFunction = UtilitiesConcrete; // Ok 

而對于 TypeScript 4.2 以下的版本,我們可以通過以下方式來解決上面的問題:

  1. type Constructor<T> = Function & { prototype: T } 
  2.  
  3. abstract class Utilities {} 
  4.  
  5. class UtilitiesConcrete extends Utilities {} 
  6.  
  7. let UtilityClass: Constructor<Utilities> = Utilities; 
  8. let UtilityClass1: Constructor<UtilitiesConcrete> = UtilitiesConcrete; 

介紹完抽象構造簽名,最后我們來簡單介紹一下 class type 與 typeof class type 的區(qū)別。

十、class type 與 typeof class type

  1. class Person { 
  2.   static cid: string = "exe"
  3.   name: string; // 成員屬性 
  4.    
  5.   static printCid() { // 定義靜態(tài)方法 
  6.     console.log(Person.cid);   
  7.   } 
  8.  
  9.   constructor(name: string) { // 類的構造函數(shù) 
  10.     this.name = name
  11.   } 
  12.  
  13.   say(words: string) :void { // 定義成員方法 
  14.     console.log(`${this.name} says:${words}`);   
  15.   } 
  16.  
  17. // Property 'say' is missing in type 'typeof Person' but required in type 'Person'
  18. let p1: Person = Person; // Error 
  19. let p2: Person = new Person("Semlinker"); // Ok 
  20.  
  21. // Type 'Person' is missing the following properties from type 'typeof Person': prototype, cid, printCid 
  22. let p3: typeof Person = new Person("Lolo"); // Error 
  23. let p4: typeof Person = Person; // Ok 

通過觀察以上的代碼,我們可以得出以下結論:

  • 當使用 Person 類作為類型時,可以約束變量的值必須為 Person 類的實例;
  • 當使用 typeof Person 作為類型時,可以約束變量的值必須包含該類上的靜態(tài)屬性和方法。

此外,需要注意的是 TypeScript 使用的是 結構化 類型系統(tǒng),與 Java/C++ 所采用的 名義化 類型系統(tǒng)是不一樣的,所以以下代碼在 TS 中是可以正常運行的:

  1. class Person { 
  2.   constructor(public name: string) {}   
  3.  
  4. class SuperMan { 
  5.   constructor(public name: string) {}   
  6.  
  7. let p1: SuperMan = new Person("Semlinker"); // Ok 

好的,在日常工作中,TypeScript 類比較常見的知識,就介紹到這里。

本文轉載自微信公眾號「全棧修仙之路」

 

責任編輯:姜華 來源: 全棧修仙之路
相關推薦

2010-03-09 17:30:08

Linux新手知識點

2025-04-01 08:25:00

OSPF網(wǎng)絡IT

2023-12-23 11:15:25

2023-12-22 15:32:20

2024-12-27 08:59:01

2019-07-11 14:45:52

簡歷編程項目

2010-03-18 13:48:14

Linux新手

2011-08-16 13:15:15

MongoDB

2018-11-16 16:13:33

5G通信技術網(wǎng)絡

2024-05-16 08:26:24

開發(fā)技巧項目

2009-06-24 10:45:42

Linux

2020-08-11 17:14:31

數(shù)據(jù)庫SQL技術

2024-07-16 10:13:01

2019-11-29 14:56:14

語音交互機器人聊天

2025-01-20 00:06:13

2019-11-12 14:50:49

Windows電腦Windows 10

2022-05-16 07:48:54

Python操作類型

2022-02-22 23:25:19

Python編程語言開發(fā)

2024-12-31 08:10:00

2024-09-09 18:18:45

點贊
收藏

51CTO技術棧公眾號