TypeScript 中的 class

https://www.jb51.net/article/138724.htm

TypeScript 中的 class

講完了 JavaScript 中的類,還是沒有用到 抽象類,抽象方法,私有方法這三個概念,由於 JavaScript 語言的侷限性,想要實現這三種概念是很困難的,但是在 TypeScript 可以輕鬆的實現這一特性。

首先我們稍微修改一下例子中的描述,Person 是抽象類,因爲一個正常的人肯定是有國籍的,Person 的 sayHello 方法是抽象方法,因爲每個國家打招呼的方式不一樣。另外一個人的性別是隻能讀取,不能修改的,且是確定的是,不是男生就是女生,所以還要藉助一下枚舉。

?

1

2

3

4

enum Gender {

 female = 0,

 male = 1

};

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

abstract class Person {

 private x: string = '私有屬性x,子類和實例都無法訪問';

 protected y: string = '私有屬性y,子類可以訪問,實例無法訪問';

 

 name: string;

 public age: number;

 public readonly gender: Gender; // 用關鍵字 readonly 表明這是一個只讀屬性

 

 public static x: string = '靜態屬性x';

 public static foo() {

  console.log(`類 ${this.name} 有一個 ${this.x}`);

 }

 

 constructor(name: string, age: number, gender: Gender) {

  this.name = name;

  this.age = age;

  this.gender = gender;

 }

 

 get fullName(): string {

  const suffix = this.gender === 1 ? '先生' : '女士';

  return this.name + suffix;

 }

 

 set FullName(value: string) {

  console.log(`你已改名爲 ${value} `);

 }

 

 // 抽象方法,具體實現交由子類完成

 abstract sayHello(): void;

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

class Chinese extends Person {

 public kungfu: string;

 public static bar() {

  console.log(`類 ${this.name} 的父類是 ${super.name}`);

  super.foo();

 }

 

 public constructor(name: string, age: number, gender: Gender, kungfu: string) {

  super(name, age, gender);

  this.kungfu = kungfu;

 }

 

 public sayHello(): void {

  console.log(`你好我是 ${this.fullName} ,我 ${this.age} 歲了`);

 }

 

 public martial() {

  console.log(`${this.name} 正在修煉 ${this.kungfu} `);

 }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

class American extends Person {

 static y = '靜態屬性y';

 public static bar() {

  console.log(`類 ${this.name} 有自己的 ${this.y} ,還繼承了父類 ${super.name} 的 ${super.x}`);

 }

 

 public twitter: string;

 

 public constructor(name: string, age: number, gender: Gender, twitter: string) {

  super(name, age, gender);

  this.twitter = twitter;

 }

 

 public sayHello(): void {

  console.log(`Hello, I am ${this.fullName} , I'm ${this.age} years old`);

 }

 

 public sendTwitter(msg: string): void {

  console.log(`${this.name} : `);

  console.log(` ${msg}`);

 }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Person.x;  // 靜態屬性x

Person.foo(); // 類 Person 有一個 靜態屬性x

 

Chinese.x;  // 靜態屬性x

Chinese.foo(); // 類 Chinese 有一個 靜態屬性x

Chinese.bar(); // 類 Chinese 的父類是 Person

 

American.x;  // 靜態屬性x

American.y;  // '靜態屬性y

American.foo(); // 類 American 有一個 靜態屬性x

American.bar(); // 類 American 有自己的 靜態屬性y ,還繼承了父類 Person 的 靜態屬性x

 

const c: Chinese = new Chinese('韓梅梅', 18, Gender.female, '詠春拳');

const a: American = new American('特朗普', 72, Gender.male, 'Donald J. Trump');

 

c.sayHello(); // 你好我是 韓梅梅女士 ,我 18 歲了

c.martial(); // 韓梅梅 正在修煉 詠春拳

a.sayHello(); // Hello, I am 特朗普先生 , I'm 72 years old

a.sendTwitter('推特治國'); // 特朗普 : 推特治國

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章