ECMAScript 6开始,JavaScript程序员将能够使用基于类的面向对象的方式。 使用TypeScript,我们允许开发者现在就使用这些特性,并且编译后的JavaScript可以在所有主流浏览器和平台上运行,而不需要等到下个JavaScript版本。
类
class Greeter {greeting: string;constructor(message: string) {this.greeting = message;}greet() {return "Hello, " + this.greeting;}}let greeter = new Greeter("world");
在类里面,使用()形式和箭头函数形式定义方法是有去别的. 我们这里建议使用原型的形式定义方法.
继承
在TypeScript里,我们可以使用常用的面向对象模式。 基于类的程序设计中一种最基本的模式是允许使用继承来扩展现有的类。
class Animal {move(distanceInMeters: number = 0) {console.log(`Animal moved ${distanceInMeters}m.`);}}class Dog extends Animal {bark() {console.log('Woof! Woof!');}}const dog = new Dog();dog.bark();dog.move(10);dog.bark();
公共,私有与受保护的修饰符
默认为public
在上面的例子里,我们可以自由的访问程序里定义的成员。 如果你对其它语言中的类比较了解,就会注意到我们在之前的代码里并没有使用public来做修饰;例如,C#要求必须明确地使用public指定成员是可见的。 在TypeScript里,成员都默认为public。
你也可以明确的将一个成员标记成public。 我们可以用下面的方式来重写上面的Animal类:
class Animal {public name: string;public constructor(theName: string) { this.name = theName; }public move(distanceInMeters: number) {console.log(`${this.name} moved ${distanceInMeters}m.`);}}
理解private
当成员被标记成private时,它就不能在声明它的类的外部访问。
即时是继承的对象,依然无法直接访问.
class Animal {private name: string;constructor(theName: string) { this.name = theName; }}class Rhino extends Animal {constructor() { super("Rhino"); }say(){// 这里无法访问 父类中的 private name属性console.log('this.name:',this.name);}}new Animal("Cat").name; // 错误: 'name' 是私有的.let animal = new Animal("Goat");let rhino = new Rhino();
理解protected
protected修饰符与private修饰符的行为很相似,但有一点不同,protected成员在派生类中仍然可以访问。
class Animal {protected name: string;constructor(theName: string) { this.name = theName; }}class Rhino extends Animal {constructor() { super("Rhino"); }say(){// 可以访问 父类 protected name属性console.log('this.name:',this.name);}}new Animal("Cat").name; // 错误: 'name' 是私有的.let animal = new Animal("Goat");let rhino = new Rhino();
readonly修饰符
你可以使用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 是只读的.
存取器
静态属性
类的静态成员,这些属性存在于类本身上面而不是类的实例上。
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 scalelet grid2 = new Grid(5.0); // 5x scaleconsole.log(grid1.calculateDistanceFromOrigin({x: 10, y: 10}));console.log(grid2.calculateDistanceFromOrigin({x: 10, y: 10}));
抽象类
把类当做接口使用
类定义会创建两个东西:类的实例类型和一个构造函数。 因为类可以创建出类型,所以你能够在允许使用接口的地方使用类。
class Point {x: number;y: number;}interface Point3d extends Point {z: number;}let point3d: Point3d = {x: 1, y: 2, z: 3};
