OOP 常见的两种实现方式: class-based 和 prototype-based

  1. class Greeter {
  2. greeting: string;
  3. constructor(message: string) {
  4. this.greeting = message;
  5. }
  6. greet() {
  7. return "Hello, " + this.greeting;
  8. }
  9. }
  10. let greeter = new Greeter("world");

Inheritance

  1. class Animal {
  2. name: string;
  3. constructor(theName: string) { this.name = theName; }
  4. move(distanceInMeters: number = 0) {
  5. console.log(`${this.name} moved ${distanceInMeters}m.`);
  6. }
  7. }
  8. class Snake extends Animal {
  9. constructor(name: string) { super(name); }
  10. move(distanceInMeters = 5) {
  11. console.log("Slithering...");
  12. super.move(distanceInMeters);
  13. }
  14. }
  15. class Horse extends Animal {
  16. constructor(name: string) { super(name); }
  17. move(distanceInMeters = 45) {
  18. console.log("Galloping...");
  19. super.move(distanceInMeters);
  20. }
  21. }
  22. let sam = new Snake("Sammy the Python");
  23. let tom: Animal = new Horse("Tommy the Palomino");
  24. sam.move();
  25. tom.move(34);

public , private , and protected modifiers

public 是默认的

Understanding private

TypeScript is a structural type system. When we compare two different types, regardless of where they came from, if the types of all members are compatible, then we say the types themselves are compatible

然而, 如果用 private 或者 protected 来修饰就有点不同了:

  1. class Animal {
  2. private name: string;
  3. constructor(theName: string) { this.name = theName; }
  4. }
  5. class Rhino extends Animal {
  6. constructor() { super("Rhino"); }
  7. }
  8. class Employee {
  9. private name: string;
  10. constructor(theName: string) { this.name = theName; }
  11. }
  12. let animal = new Animal("Goat");
  13. let rhino = new Rhino();
  14. let employee = new Employee("Bob");
  15. animal = rhino;
  16. animal = employee; // Error: 'Animal' and 'Employee' are not compatible

因为:

  1. Animal and Rhino share the private side of their shape from the same declaration of private name: string in Animal, they are compatible

  2. Even though Employee also has a private member called name, it’s not the one we declared in Animal

Understanding protected

和 private 差不多, 只是能在子类里使用父类的 protected
还有一个很有趣的点就是把 protected 用在父类的 constructor 上

  1. class Person {
  2. protected name: string;
  3. // 这样就不能直接 new Person() 了, 只能用来当父类进行继承
  4. protected constructor(theName: string) { this.name = theName; }
  5. }
  6. // Employee can extend Person
  7. class Employee extends Person {
  8. private department: string;
  9. constructor(name: string, department: string) {
  10. super(name);
  11. this.department = department;
  12. }
  13. public getElevatorPitch() {
  14. return `Hello, my name is ${this.name} and I work in ${this.department}.`;
  15. }
  16. }
  17. let howard = new Employee("Howard", "Sales");
  18. let john = new Person("John"); // Error: The 'Person' constructor is protected

readonly modifier

readonly 的属性只能在声明的时候或者在构造函数里赋值

  1. class Octopus {
  2. readonly name: string;
  3. readonly numberOfLegs: number = 8;
  4. constructor (theName: string) {
  5. this.name = theName;
  6. }
  7. }
  8. let dad = new Octopus("Man with the 8 strong legs");
  9. dad.name = "Man with the 3-piece suit"; // error! name is readonly.
  10. Parameter properties

有种简写方式, 能让属性声明再在构造函数里赋值这两部操作合成一步:

  1. // 就是直接在构造函数的参数里加上对应的修饰符
  2. // 同理, 把 readonly 换成 public, protected, private 都是一样的
  3. class Octopus {
  4. readonly numberOfLegs: number = 8;
  5. constructor(readonly name: string) {
  6. }
  7. }

Accessors

就是 getter 和 setter
只带 getter 不带 setter 的属性默认会标记为 readonly 的

Static Properties

加 static 关键字

Abstract Classes

抽象类用作基类
和 interface 不同的是, 它可以包含方法的实现细节
抽象方法不包含具体实现, 并且必须在子类实现

  1. abstract class Animal {
  2. abstract makeSound(): void;
  3. move(): void {
  4. console.log('roaming the earch...');
  5. }
  6. }