要从Element
in继承功能Wind
,您需要扩展或混合Element
in Wind
。仅仅实现一个接口不会继承任何实现。
所以,你需要有class Wind extends Element { ... }
. 目前这是不可能的,因为Element
没有Wind
可以用作超级构造函数的生成构造函数。因此,您也需要添加它,并确保action
在该构造函数中初始化该字段。
class Element {
final String action;
Element._(this.action); // Generative constructor that Wind can use.
factory Element() = Wind; // Factory constructor creating a Wind.
String act() => action;
}
class Wind extends Element {
Wind() : super._("blows");
}
生成构造函数不需要是私有的,但如果您只在自己的库中声明和使用所有类,它也可能是私有的。
另一种选择是拥有一个包含字段和函数的单独ElementBase
类以及一个空名称的生成构造函数。在这种情况下,mixins 不是一个好的选择,因为当 mixins 不能有构造函数时,没有很好的方法来做final。action
act
action
abstract class Element {
String get action;
factory Element() = Wind;
String act();
}
class ElementBase implements Element {
final String action;
ElementBase(this.action);
String act() => action;
}
class Wind extends ElementBase {
Wind() : super("blow");
}
想要子类的生成构造函数和工厂构造函数在接口/骨架类中生成默认实现是一个常见问题。List
andMap
接口有这个问题,通过暴露andListBase
解决了MapBase
。当您将超类公开给其他库中的其他用户时,我认为这是最好的解决方案。如果它只是你自己内部使用,我将在超类中使用私有/非默认命名的生成构造函数。