以下代码来自 Mixins in Dart ,尽管缺少 super 接口(interface)方法 twice()
,但 mixin K
的应用不会产生警告:
class S {
twice(int x) => 2 * x;
}
abstract class I {
twice(x);
}
abstract class J {
thrice(x);
}
class K extends S implements I, J {
int thrice(x) => 3* x;
}
class B {
twice(x) => x + x;
}
class A = B with K;
class D {
double(x) => x+x;
}
// E missing 'twice', thus not conforming to
// superinterface S of K. Yet no warning in
// checked mode under Dart 1.13.
class E = D with K;
main() {
/// A conforms to superinterface S of K
assert(new A().twice (1) == 2);
assert(new A().thrice(1) == 3);
/// E conforms to superinterface S of K
//assert(new E().twice (1) == 2); // NoSuchMethod 'twice'
assert(new E().thrice(1) == 3); // thrice() Provided by K
}
我主要只是想填补我理解中的一些漏洞,因此希望有人指出我的问题中任何术语的误用或不好的概念。
最佳答案
Dart 1.13 版本中 mixin 的工作方式发生了变化。它已在VM中实现,但尚未在dart2js中实现,并且我不确定分析器。
不过,E
是一个非抽象类,它没有实现自己的接口(interface)(实现 I
但没有两次非抽象
成员)。这应该会导致警告、混合或不混合。
最重要的是,K
扩展了 S
,因此当使用 K
作为 mixin 时,mixin 应用程序的父类(super class)也应该实现 S
而 D
则不然。这应该会再次发出警告。
关于inheritance - Dart 1.13 是否放宽了 mixin 应用程序的 super 接口(interface)要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33980464/