typescript - 如何访问“root”类方法

时间:2018-01-29 08:38:07

标签: typescript

我从其他类继承的类继承了其他类,如下所示:

class RootClass {
  call(): void;
}

class SubClass extends RootClass {
  call(): void:
}

class MyClass extends SubClass {
  call(): void {
    RootClass.call();  // how to access parent class's parent class?
  }
}

似乎C++可以执行以下操作:RootClass::call()call方法不是静态的。

我知道有super来访问基类的属性和方法,无论如何直接访问root类方法?

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用prototype的{​​{1}}访问该功能,并使用RootClasscall

为其调用此功能
apply

答案 1 :(得分:1)

静态和实例调用将如下所示:

class RootClass {
   call(): void {
      console.log("RootClass")
  }

  static staticCall(): void {
    console.log("RootClass static")
  }
}

class SubClass extends RootClass {
  call(): void {
      super.call();  // run instance method of RootClass
      console.log("SubClass")
  }
}

class MyClass extends SubClass {
  call(): void {
    super.call();  // run instance method of SubClass
    RootClass.staticCall(); // run static method of RootClass
  }
}

var m = new MyClass();
m.call();