从类

时间:2017-07-08 18:54:26

标签: angular typescript typescript2.0

我有一个基类有两种方法," A"和" B"。

方法" B"电话" A"在基地。然后我有一个子类,它会覆盖" A"方法。当我打电话给方法" B"从子类的方法" A"来自基地被称为。

super有没有办法从子类调用重写的方法?

export class SuperClass {
    A(param: any): string {
        return "A called from base";
    }

    B(param: any): string {
        let value = this.A(param);
        console.log(value);

        return "B called from base";
    }
}

export class SubClass extends BaseClass  {
    A(param: any): string {
        return "A called from subclass";
    }
}

// create a subclass
let bInstance = new B();
bInstance.B(someParam); // I want to call the overriden A in the subclass

PD:" A"在基类中需要有自己的实现,不能是抽象的

1 个答案:

答案 0 :(得分:3)

  

bInstance.B(someParam); //我想调用覆盖的A.   子类

它将在子类中调用重写的方法A,因为this将指向SubClass的实例,JS将在此寻找方法A实例第一。所以这段代码:

B(param: any): string {
    let value = this.A(param);
    console.log(value);

应该记录"A called from subclass";