飞镖码:
main() {
var child1 = new Child1();
var t = child1.childType();
}
class Parent {
??? childType() {
return this.runtimeType;
}
}
class Child1 extends Parent {
}
class Child2 extends Parent {
}
您可以在班级???
中看到Parent
,我希望它能引用孩子的类型,但我不知道如何申报。
在scala中,它可以是:
def childType(): this.type = {
...
}
但我不知道如何在飞镖中做到这一点。可能吗?如果不可能,这里使用的最佳类型是什么?
答案 0 :(得分:3)
如果您确实需要将childType
的返回类型静态声明为正确的子类型,则可以使用泛型:
class Parent<C extends Parent> {
C get childType => runtimeType;
}
class Child1 extends Parent<Child1> {}
class Child2 extends Parent<Child2> {}
我确实要确保你需要它。您可以将childType
键入为Parent
。