inheritance - Dart 1.13 是否放宽了 mixin 应用程序的 super 接口(interface)要求?

标签 inheritance dart mixins

以下代码来自 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)也应该实现 SD 则不然。这应该会再次发出警告。

关于inheritance - Dart 1.13 是否放宽了 mixin 应用程序的 super 接口(interface)要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33980464/

相关文章:

dart - 如何从 Dartium DevTool 控制台访问 Dart 函数/变量?

css - 在 SASS 中,我可以在媒体查询和选择器中包装一堆样式规则吗

java - 为什么我在使用 jackson 时会收到 stackoverflow 错误,即使使用 @JsonIgnoreProperties

iphone - 在 Objective C 中需要多重继承

java - 为什么有人会强制转换而不是返回子类型

flutter - 此参数在Flutter文档中传递给此回调函数的位置是什么?

flutter - 如何将键值对列表推送/插入到 dart/flutter 中的列表中?

c++ - 了解 Google 的 V8 C++ 代码库中的继承

c++ - 派生类不调用基类的成员函数

sass - SCSS 重复值?