类就是用来创造对象的东西。它和interface的区别是:interface实现了class的一部分功能,class是interface的高配版本(个人理解),对于使用过 TS 的 JS 程序员来说,类可以让你的系统更加「可预测」这个对象不会出现一些我不知道的属性,一切都尽在我的掌握。
### 语法
- 声明类(class)
- 声明对象的非函数属性
- 声明对象的函数属性
- 使用 constructor
- 声明类的属性(static)指的是class的静态属性,static申明的属性属于class的,不属于实例的的属性
- 使用 this 代指当前对象(注意不要以为 this 永远都代指当前对象,JS 的 this 有更多功能,而且默认 this 为 window)
### 类继承类
使用 super
~~~js
class A{
type:string;
constructor(type:string){
this.type = type
}
}
class B extends A {
constroctor(){
super('xxx')
}
}
const b = new B()
console.log(b) // { type: 'xxx' }
~~~
### 修饰符
public : 默认的属性都是public 的
private :私有属性 不能在在实例化后的实例中使用这个属性 , 只能在class中共创建使用 类似于局部变量
protected :只能在自己和子代中使用的属性,实例中不会显示此属性
### 访问器
get 和 set
###