我在 Dart 代码中有以下继承结构,并且想知道是否有一个方法签名可以同时考虑 T 的类 A 和类 B,这样我就可以进行静态检查而不必进行类型转换。这可能吗?
abstract class A {
String one;
}
abstract class B {
String two;
}
class C implements A, B {
String one;
String two;
}
class D implements A, B {
String one;
String two;
}
void _ohNo<T extends A, B>(T t) {
print(t.one); // valid
print(t.two); // invalid: getter not defined
}
void _ohNo2<T extends A, T extends B>(T t) {} // invalid: T already defined
Run Code Online (Sandbox Code Playgroud)
可能是一个解决方法:
abstract class Both extends A with B {}
class C implements Both {
String one = '';
String two = '';
}
class D implements Both {
String one = '';
String two = '';
}
void ohNo<T extends Both>(T t) {
print(t.one);
print(t.two);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
437 次 |
| 最近记录: |