TypeScript 从扩展 class 函数覆盖中调用基础 class 函数
TypeScript call base class function from within extended class function override
如果我有一个覆盖基础 class 函数的扩展 class,我可以在扩展 class 函数中调用基础 class 函数吗?类似于:
class Base {
DoSomething() {
console.log("base class function");
}
}
class Derived extends Base {
DoSomething() {
this.base.DoSomething();
console.log("extended class function");
}
}
Derived.DoSomething() 的输出理想情况下如下所示:
"base class function"
"extended class function"
这在 TypeScript 中可行吗?
是的,您可以通过 super keyword
访问父方法
class Base {
foo() {
console.log("base class function");
}
}
class Derived extends Base {
foo() {
super.foo();
console.log("extended class function");
}
}
(new Derived()).foo()
如果我有一个覆盖基础 class 函数的扩展 class,我可以在扩展 class 函数中调用基础 class 函数吗?类似于:
class Base {
DoSomething() {
console.log("base class function");
}
}
class Derived extends Base {
DoSomething() {
this.base.DoSomething();
console.log("extended class function");
}
}
Derived.DoSomething() 的输出理想情况下如下所示:
"base class function"
"extended class function"
这在 TypeScript 中可行吗?
是的,您可以通过 super keyword
访问父方法class Base {
foo() {
console.log("base class function");
}
}
class Derived extends Base {
foo() {
super.foo();
console.log("extended class function");
}
}
(new Derived()).foo()