typescript的链式调用
抄一遍
class Calculator {
  private value: number;

  constructor(initialValue: number) {
    this.value = initialValue;
  }
  
  /**
   * this 告诉 TypeScript:这个方法返回“当前类的实例类型”,从而支持链式调用和继承.
   */
  add(num: number): this {
    this.value += num;
    return this;
  }

  subtract(num: number): this {
    this.value -= num;
    return this;
  }

  multiply(num: number): this {
    this.value *= num;
    return this;
  }

  divide(num: number): this {
    this.value /= num;
    return this;
  }

  getValue(): number {
    return this.value;
  }
}

const result = new Calculator(10)
  .add(5)
  .multiply(2)
  .subtract(3)
  .divide(4)
  .getValue();

console.log(result); // 输出 6.75