我的所有层次结构中都有一些静态的东西(在这个例子中,_image
)。我希望能够访问通讯员_image
而不必重复代码:
这会很棒:
class Actor {
static _image; // I need it to be static
method show(){ // I need it to be non-static
this.setImage(this.class._image); //doesn't work....
}
}
class GoodActor extends Actor {
static _image = 'good.png'
}
class BadActor extends Actor {
static _image = 'bad.png'
}
class MediumActor extends Actor {
static _image = 'medium.png'
}
但它不起作用。现在我只需要:
class Actor {
}
class GoodActor extends Actor {
static _image = 'good.png' // I need it to be static
method show(){ // I need it to be non-static
this.setImage(GoodActor._image);
}
}
class BadActor extends Actor {
static _image = 'bad.png' // I need it to be static
method show(){ // I need it to be non-static
this.setImage(BadActor._image);
}
}
class MediumActor extends Actor {
static _image = 'medium.png' // I need it to be static
method show(){ // I need it to be non-static
this.setImage(MediumActor._image);
}
}
假设这四个类有更多的方法。我不想show()
在每个子类中重复该方法......但是我需要该show()
方法是非静态的并且_image
是静态访问的。
我已经阅读了这个问题https://github.com/Microsoft/TypeScript/issues/7673但不幸的是我不能在那里问,因为他们没有修复它就关闭了它。他们都没有谈到需要动态解析要调用的静态方法的问题。