简单总结为两点(更多参考:ES6中类的写法、TS中类的写法)
- TS中函数参数需要写类型
- TS中构造函数中的参数需要提前声明 ``` class ES6Point { constructor(x, y) { this.x = x; this.y = y; } }
class TSPoint { x: number; y: number; constructor(x:number, y:number) { this.x = x; this.y = y; } }
<a name="L3ZSp"></a>#### <br />在TS中类的属性的修饰符有private、protected、public,属性默认类型就是public,可以给构造函数增加public修饰符的方式来简化书写:```jsonclass TSPoint {constructor(public x:number, public y:number) {this.x = x;this.y = y;}}
static属性声明的属性,只有通过类可以访问
class Dog {constructor(public name: string) {this.name = name;}run(){}static isTest: boolean = true;}console.log(Dog.isTest); // truelet dog: Dog = new Dog('wangcai'); // {run: ƒ, constructor: ƒ}console.log(dog); // Dog {name: "wabg"}
TS中的抽象类是只可以继承 不可以实例化的类,它的方法可以实现,也可以是抽象的。
abstract class Animal {bak() {console.log('what sound')}}let a: Animal = new Animal(); // Cannot create an instance of an abstract class.
