作者:风铃草549天王 | 来源:互联网 | 2023-07-16 14:03
Ihaveaninheritancehierarchyinatypescriptapplicationthatresemblesthefollowing:我在typescr
I have an inheritance hierarchy in a typescript application that resembles the following:
我在typescript应用程序中有一个类似于以下内容的继承层次结构:
class A {
someProp: any;
constructor(someObj: any){
}
}
class B extends class A {
constructor(someObj: any){
super(someObj);
}
public doStuff(){
console.log("doing stuff!");
}
}
In a second file, I attempt to call methods on the subclass after instantiating it like so:
在第二个文件中,我尝试在实例化之后调用子类上的方法,如下所示:
var instanceB: A;
...
instanceB = new B(someObj);
instanceB.doStuff(); // produces error symbol cannot be resolved, it is probably located in an inaccessible module
So what am I doing wrong? As far as I understand prototypal inheritance in Javascript, the method will be searched for in the hierarchy of regardless of where it is defined.
那么我做错了什么?据我所知,Javascript中的原型继承,无论在何处定义,都将在层次结构中搜索该方法。
As a workaround, I've added an abstract method in the base class, and then I provide the implementation in the subclass. The problem with this is that I need to be able to swap one subclass for another depending on the application state. And to me, it seems unnecessary to define a method on the parent class that all subclasses need not implement.
作为一种解决方法,我在基类中添加了一个抽象方法,然后我在子类中提供了实现。这个问题是我需要能够根据应用程序状态将一个子类交换为另一个子类。对我来说,似乎没有必要在父类上定义一个所有子类都不需要实现的方法。
1 个解决方案