繁体   English   中英

如何获取基类(Angular2 +)中的派生类名称?

[英]How to get the derived class name in the base class (Angular2+)?

当我有一个扩展基类的类时,如何获得派生类的名称(在基类中)?

我在基类中有一个被派生类调用的方法。 我想知道哪个实例正在调用该方法。 使用this.constructor.name可以得到当前的基类,而不是派生的基类。

角4/5

谢谢。

[更新]

  • 我正在使用 .funcInBaseClass()和超级 .funcInBaseClass()在基类中调用该函数。
  • 该功能是“受保护的”

您需要使用构造函数类的name属性。

class Base {   
    public process(value: any) {
        alert(this.constructor["name"]); // alerts 'Derived' in this example
    }
}

class Derived extends Base {
    public process(value: any) {
        super.process(value); // use super to call methods on the base class
    }
}

(new Derived).process('someValue');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM