class
1 类的定义
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("world");
2 类的继承
super()
函数: 派生类包含了一个构造函数,在构造函数里访问 this的属性之前, 必须调用 super()
,来执行基类的构造函数。
class Animal{
name: string;
constructor(name:string){
this.name = name;
}
move(distance:number = 0){
console.log('move ' + distance + ' m');
}
}
class Dog extends Animal{
name: string;
id:number;
constructor(name:string, id:number){
super(name);
this.id = id;
}
bark(){
console.log("woolf woolf " + this.name + " " + this.id);
}
}
let dog = new Dog("123", 345);
dog.move();
dog.bark();
3 类的成员
类的成员分为两类 实例成员 和 静态成员。
3.1 类的实例成员权限
3.1.1 public private protected
- 成员都默认为 public
- private 成员无法在类外访问
- protected 成员在派生类中仍然可以访问
- 类型比较
- public: 成员的类型兼容,那么类就是兼容的。
- private , protected 同一个类或者继承类的实例,才认为是兼容的。
- (关于类型比较)参考原话:
- TypeScript使用的是结构性类型系统。 当我们比较两种不同的类型时,并不在乎它们从何处而来,如果所有成员的类型都是兼容的,我们就认为它们的类型是兼容的。
- 如果其中一个类型里包含一个 private成员,那么只有当另外一个类型中也存在这样一个 private成员, 并且它们都是来自同一处声明时,我们才认为这两个类型是兼容的。 对于 protected成员也使用这个规则。
3.1.2 readonly
-
使用: 声明时或构造函数中
class Octopus { readonly name: string; readonly numberOfLegs: number = 8; constructor (theName: string) { this.name = theName; } } let dad = new Octopus("Man with the 8 strong legs"); dad.name = "Man with the 3-piece suit"; // 错误! name 是只读的.
-
参数属性: 无需另外声明。(如果是public,是误会把该变量变成类的成员)
class Octopus { readonly numberOfLegs: number = 8; constructor(readonly name: string) { } }
3.2 类的静态成员
-
设置 通过类名 来方位静态成员。例如:
Grid.origin
。class Grid { static origin = {x: 0, y: 0}; calculateDistanceFromOrigin(point: {x: number; y: number;}) { let xDist = (point.x - Grid.origin.x); let yDist = (point.y - Grid.origin.y); return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale; } constructor (public scale: number) { } } let grid1 = new Grid(1.0); // 1x scale console.log(grid1.calculateDistanceFromOrigin({x: 10, y: 10}));
-
修改
- typeof Greeter,意思是取Greeter类的类型,而不是实例的类型。 或者更确切的说,“告诉我 Greeter标识符的类型”,也就是构造函数的类型。 这个类型包含了类的所有静态成员和构造函数。
let gridMaker: typeof Grid = Grid; gridMaker.origin = {x: 0.5, y: 0.5}; let grid2 = new Grid(1.0); // 1x scale console.log(grid2.calculateDistanceFromOrigin({x: 10, y: 10}));
4 抽象类: abstract
抽象类中的抽象方法不包含具体实现并且必须在派生类中实现。
abstract class Animal {
abstract makeSound(): void;
move(): void {
console.log('roaming the earch...');
}
}
5 存储器
TypeScript支持通过getters/setters来截取对对象成员的访问。 它能帮助你有效的控制对对象成员的访问。
-
没有使用存储器
class Employee { fullName: string; } let employee = new Employee(); employee.fullName = "Bob Smith"; if (employee.fullName) { console.log(employee.fullName); }
-
使用存储器: 在赋值语句和输出语句中自动调用get 和 set 设置的函数
let passcode = "secret passcode"; class Employee { private _fullName: string; get fullName(): string { return this._fullName; } set fullName(newName: string) { if (passcode && passcode == "secret passcode") { this._fullName = newName; } else { console.log("Error: Unauthorized update of employee!"); } } } let employee = new Employee(); employee.fullName = "Bob Smith"; if (employee.fullName) { alert(employee.fullName); }