# TS中的类

WARNING

  • 基础
  • 修饰符
  • readonly 修饰符
  • 参数属性
  • 静态属性
  • 可选类属性
  • 存取器
  • 抽象类
  • 实例类型

基础使用

修饰符 public 表示公共的,创建实例后,可以通过实例访问的, 不写修饰符的情况下默认是 public


class Point {
	x: number
	y: number
	constructor(x: number, y: number) {
		this.x = x
		this.y = y
	}
	getPosition () {
		return `${this.x} ${this.y}`
	}
}

let p1 = new Point(1, 3)
console.log(p1.x)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

修饰符 private 表示私有的,只允许在当前类内使用,在类的外面是无法访问的


class Parent {
	private age: number
	constructor(age: number) {
		// 类内可以访问私有属性
		this.age = age
	}
}

let p2 = new Parent(12)
console.log(p2.age) // 提示没有这个属性
console.log(Parent.age) // 类上也没有这个属性


class Child extends Parent {
	constructor(age: number) {
		super(age);
		console.log(super.age) // 提示父类把age设置成受保护的了
	}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

修饰符 protected 表示受保护,允许在类内以及继承的子类中使用


class Parent2 {
	protected age: number
	constructor(age: number) {
		this.age = age
	}
	protected getAage() {
		return this.age
	}
}

class Child2 extends Parent2 {
	constructor(age: number) {
		super(age);
		console.log(super.age, '父类受保护的属性') // undefined
		console.log(super.getAage(), '父类受保护的方法') // 122
	}
}
let p2 = new Parent2(12)
// console.log(p2.age) // 无法访问
let c2 = new Child2(122)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

修饰符 readonly

class UserInfo {
	readonly name: string
	constructor(name: string) {
		this.name = name
	}
}

let userInfo = new UserInfo('12')
console.log(userInfo)
// userInfo.name = '哈哈哈' // 提示只读
1
2
3
4
5
6
7
8
9
10

参数属性, 可以给参数设置 publicprivateprotectedreadonly


class A {
	constructor(private name: string) {
	}
}

let a = new A('123')
console.log(a.name) // 无法访问


1
2
3
4
5
6
7
8
9
10

静态属性 static, 实例不会添加也不会继承静态属性。

class Parent3 {
	static age: number = 18
	static getAge() {
		return Parent3.age
	}
}

let p3 = new Parent3()
// console.log(p3.age) // 无法获取
console.log(Parent3.age) // 18
console.log(Parent3.getAge()) // 18
1
2
3
4
5
6
7
8
9
10
11

可选类属性 ?

class Info2 {
	name: string = '12'
	age?: number = 19
	
	constructor(name: string, age?: number, sex?: string) {
		this.name = name
		this.age = age
	}
	
}

let info2 = new Info2('12')
console.log(info2, 'info2')
1
2
3
4
5
6
7
8
9
10
11
12
13

存取器(get 和 set)

class Info2 {
	name: string = '12'
	age?: number = 19
	private _infoStr: string = ''
	constructor(name: string, age?: number, sex?: string) {
		this.name = name
		this.age = age
	}
	get infoStr() {
		return `${this.name} ${this.age}`
	}
	set infoStr(val) {
		console.log('要设置的值', val)
		this._infoStr = val
	}
}

let info2 = new Info2('12', 90)
console.log(info2, 'info2')
console.log(info2.infoStr, 'info2.infoStr') // 12 90
info2.infoStr = '50'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

抽象类:abstract 抽象类, 把类里的通用的方法属性抽象出来, 不能直接创建实例,只能被类继承。

abstract class People {
	constructor(name: string) {
	}
	abstract printName(): void
}

class ManPepple extends People {
	name: string
	constructor(name: string) {
		super(name);
		this.name = name
	}
	// 需要自己实现下抽象类里的方法和属性
	printName() {
		console.log(this.name)
	}
}

let m = new ManPepple('你好')
console.log(m.name)
console.log(m.printName())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
abstract class People2 {
	abstract _name: string
	abstract get insideName(): string
	abstract set insideName(val: string)
}

class P extends  People2 {
	get insideName(): string {
		return typeof this._insideName === "string" ? this._insideName : '';
	}
	set insideName(value: string) {
		this._insideName = value;
	}
	_name: string = '小鱼'
	private _insideName: string | undefined
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

接口和类

类实现接口


interface FoodInterface {
	type: string
}

class FoodClass implements FoodInterface {
	type: string = '小鱼'
}


1
2
3
4
5
6
7
8
9
10

接口继承类

class AA {
	protected name: string = '小鱼'
}

interface  I extends  AA {}
class B extends AA implements I {
	name: string = '小胖胖'
}

1
2
3
4
5
6
7
8
9