类的定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class P {
name: string
age: number

constructor(naem: string, age: number) {
this.name = name
this.age = age
}

play() {
console.log(this.name + '在打来福')
}
}

const p = new P('常威', 18)
p.play()

类的继承

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
class P {
name: string
age: number

constructor(naem: string, age: number) {
this.name = name
this.age = age
}

eating() {
console.log('在吃饭')
}
}

class CW extends P {
idCard: number

constructor(name: string, age: number, idCard: number) {
// 调用父类的构造器
super(name, age);
this.idCard = idCard
}

eating() {
super.eating() // 会先执行父类中的eating方法
console.log('常威在吃山珍海味')
}

play() {
console.log(this.name + '在打来福')
}
}

class LF extends P {
sno: number

constructor(name: string, age: number, sno: number) {
// 调用父类的构造器
super(name, age);
this.sno = sno
}

eating() { // 不会执行父类中的eating方法
console.log('来福在吃饭')
}

beaten() {
console.log(this.name + '被常威打')
}
}

const cw = new CW('常威', 18, 001)
cw.eating()
cw.play()
const lf = new LF('来福', 18, 9527)
lf.eating()
lf.beaten()

类的多态

父类引用(类型)指向子类对象; 多态的目的是为了写出更具通用性的代码

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
class P {
action() {
console.log('有行动')
}
}

class CW extends P {
action() {
console.log('常威在打来福')
}
}

class LF extends P {
action() {
console.log('来福在被常威打')
}
}

function makeActions(p: P[]) {
p.forEach(item => {
item.action()
})
}

makeActions([new CW(), new LF()])

类的成员修饰符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class P {
// public修饰符是在任何地方可见, 公有的属性或者方法, 默认编写的属性就是public
public name: string = '常威'

// private修饰符是仅在同一个类中可见, 私有的属性或者方法
private age: number = 18
// 使用private后可以通过写一个方法提供外部访问
getAge() {
return this.age
}
// 使用private后可以通过写一个方法提供外部修改
setAge(newAge) {
this.age = newAge
}

// protected 修饰符是仅在类自身和子类中可见, 受保护的属性或方法; 和private有点类似
}

只读属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 只读可以在构造器中赋值,赋值之后不可修改
class P {
readonly name: string
age?: number
//属性本身不能进行修改, 但如果它是对象类型, 对象中的属性是可以修改的
readonly friend?: P

constructor(name: string, friend?: P) {
this.name = name
this.friend = friend
}
}

const p = new P('常威', new P('来福'))
console.log(p.name)
console.log(p.friend)

if (p.friend) {
p.friend.age = 18
}

getters和setters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class P {
private _naem: string

constructor(name: string) {
this._name = name
}

// 访问器
set name(newName) {
this._name = newName
}
get name() {
return this._name
}
}

const p = new P('常威')
p.name = '来福'
console.log(p.name)

类的静态成员

1
2
3
4
5
6
7
8
9
10
class P {
static time: string = '12:00'

static eating() {
console.log('到点了, 干饭了')
}
}

console.log(P.time)
P.eating()

抽象类

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
abstract class Shape {
abstract getArea()
}

class Rectangle extends Shape {
private width: number
private height: number

constructor(width: number, height: number) {
super();
this.width = width
this.height = height
}

getArea() {
return this.width * this.height
}
}

class Circle extends Shape {
private r: number

constructor(r:number) {
super();
this.r = r
}

getArea() {
return this.r * this.r * 3.14
}
}

function makeArea(shape: Shape) {
return shape.getArea()
}

const rectangle = new Rectangle(20, 30)
const circle = new Circle(10)

console.log(makeArea(rectangle))
console.log(makeArea(circle))

类的类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class P {
name: string = '常威'
eating() {
console.log('常威在吃饭')
}
}

const p: P = {
name: '常威',
eating() {
console.log('常威在吃好吃的')
}
}

function fn (p: P) {
console.log(p.name)
}

fn(new P)
fn({name: '常威', eating() {console.log('常威在吃山珍海味')}})