mixin 其 super class 不是 Object

mixin whose super class is not Object

我很想做下面这样的事情

abstract class A{}                      
abstract class B extends A{}
abstract class C extends A{}
abstract class D extends B with C{} //C cannot be used as a mixin because it extends a class other than object

除了将C的内容复制到D中,还有什么办法吗?

我 类 的真实姓名,让您了解我正在尝试做什么

//A Observable
//B DynamicObservable
//C ObservableWithValidationErrors
//D DynamicObservableWithValidationErrors

对于可以用作 mixin 的 class 有一些限制(参见 Mixins in Dart - Syntax and semantics)。

However, in this proposal, a mixin may only be extracted from a class that obeys the following restrictions:

  1. The class has no declared constructors.
  2. The class’ superclass is Object.
  3. The class contains no super calls.

将来可能会取消这些限制。

The semantics are deliberately restricted in several ways, so as to reduce disruption to our existing implementations, while allowing future evolution toward a full-fledged mixin implementation. This restricted version already provides considerable value.

在某些情况下,可能会重组您的 class 以使用多个 mixin:

abstract class Observable{}
abstract class Dynamic{}
abstract class ValidationErrors{}        
abstract class DynamicObservable extends Observable with Dynamic{}
abstract class ObservableWithValidationErrors extends Observable with ValidationErrors{}
abstract class DynamicObservableWithValidationErrors extends Observable with Dynamic, ValidationErrors{}

当然,如果DynamicValidationErrors不能这样隔离,要依赖继承Observable,这就不行了。