-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9、类.ts
141 lines (119 loc) · 3.18 KB
/
9、类.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// 类的属性与方法
/*
静态属性与方法只能类来访问,属性方法是定义在类自身上的,可以被继承
成员方法其实是定义到类的原型链上的,所以子类,实例都能访问
*/
class Greeter {
// 静态属性
static cname: string = 'Greeter';
// 成员属性
greeting: string;
constructor(message: string) {
this.greeting = message
}
// 静态方法
static getClassName() {
return 'Greeter'
}
// 成员方法
greet() {
return 'hello' + this.greeting
}
}
let greeter = new Greeter("world");
// 2、ECMAScript 私有字段
/*
私有字段以 # 字符开头,有时我们称之为私有名称;
每个私有字段名称都唯一地限定于其包含的类;
不能在私有字段上使用 TypeScript 可访问性修饰符(如 public 或 private);
私有字段不能在包含的类之外访问,甚至不能被检测到。
*/
class Person {
#name: string;
constructor(name: string) {
this.#name = name
}
greet() {
console.log('hello,my name is ' + this.#name)
}
}
let p = new Person('ppp')
// p.#name; // 属性 "#name" 在类 "Person" 外部不可访问,因为它具有专用标识符。
p.greet() // hello,my name is ppp
// 3、访问器 getter setter
let passcode: string = 'Hello Typescript';
class Employee {
private _fullName: string;
get fullName(): string {
return this._fullName
}
set fullName(newName: string) {
if (passcode && passcode === "Hello Typescript") {
this._fullName = newName;
} else {
console.log('ERROR: Unauthorized update of employee!')
}
}
}
let employee = new Employee();
employee.fullName = 'Semlinker';
if (employee.fullName) {
console.log(employee.fullName);
}
// 4、类的继承 extends
class Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
move(distanceInMeters: number = 0) {
console.log(`${this.name} move ${distanceInMeters}m.`)
}
}
class Snake extends Animal {
constructor(name: string) {
super(name)
}
move(distanceInMeters: number = 5) {
console.log('Slithering...')
super.move(distanceInMeters);
}
}
let sam = new Snake("Sammy the Python");
sam.move();
// 5、抽象类
/*
使用 abstract 关键字申明的类称为抽象类。抽象类不能被实例化。
因为它里面包含一个或多个抽象方法
*/
abstract class Person1 {
constructor(public name: string) { };
// 抽象方法
abstract say(words: string): void
}
// const logo = new Person1() // error
class Developer extends Person1 {
constructor(name: string) {
super(name)
}
say(words: string): void {
console.log(`${this.name} says ${words}`)
}
}
const lolo = new Developer('lolo')
lolo.say('i love ts')
// 6、类方法的重载
class ProductService {
getProducts(): void;
getProducts(id: number): void;
getProducts(id?: number) {
if (typeof id === 'number') {
console.log(`获取id为 ${id} 的产品信息`)
} else {
console.log(`获取所有的产品信息`);
}
}
}
const productService = new ProductService();
productService.getProducts(666); // 获取id为 666 的产品信息
productService.getProducts(); // 获取所有的产品信息