如何在包含工厂方法时访问抽象超类实现? [英] How to access abstract superclass implementation when it contains a factory method?

查看:168
本文介绍了如何在包含工厂方法时访问抽象超类实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有返回一个子类的实例的工厂的抽象超类。是否可能有一个方法只在超类中实现?在下面的代码中,例如,是否可以删除Wind :: act()?

I have an abstract superclass with a factory that returns an instance of a subclass. Is it possible to have a method that is implemented only in superclass? In the following code, for instance, would it be possible to remove Wind::act()?

abstract class Element {
  final String action;    // what it does
  String act() => action; // do it

  factory Element() {
    return new Wind();
  }
}

class Wind implements Element {
  final action = "blows";
  act() => action;  // Why is this necessary?
}

void main() {
  print(new  Element().act());
}



当删除Wind :: act()时, 。此外,当扩展而不是实现超类时,省略​​子类实现不会导致错误。但是使用工厂方法,扩展不是一个选项。

When removing Wind::act(), there is an error about it missing. Also, when extending rather than implementing the superclass, leaving out the subclass implementation doesn't cause an error. But with a factory method, extending is not an option.

推荐答案


中的元素,您需要扩展或混合元素 code> Wind

To inherit functionality from Element in Wind, you need to either extend or mix-in Element in Wind. Merely implementing an interface will not inherit any implementation.

所以,你需要有 class Wind extends Element {...}
目前不可能,因为元素没有生成式构造函数, Wind 可以用作超级构造函数。因此,您还需要添加它,并确保在该构造函数中初始化操作字段。

So, you need to have class Wind extends Element { ... }. That's not currently possible because Element has no generative constructor that Wind can use as super-constructor. So, you need to add that too, and make sure to initialize the action field in that constructor.

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");
}

生成构造函数不需要是私有的,

The generative constructor doesn't need to be private, but if you are declaring and using all the classes only inside your own library, it might as well be.

另一个选择是有一个单独的 ElementBase 类包含操作字段和 act 函数和空名生成构造函数。 Mixins在这种情况下不是一个好的选择,因为没有好的方法使 action final不能有构造函数。

Another option is to have a separate ElementBase class containing the action field and act function and an empty-named generative constructor. Mixins are not a good choice in this case, because there is no good way to make action final when mixins can't have constructors.

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 Map 接口有这个问题,并通过暴露 ListBase MapBase 。我认为这是最好的解决方案,当你将超类暴露给其他库中的其他用户。如果它只在内部使用,我将使用超类中的私有/非默认命名生成构造函数。

It's a common problem to want both a generative constructor for subclasses and a factory constructor generating the default implementation in an interface/skeleton class. The List and Map interfaces have this problem, and have solved it by exposing ListBase and MapBase. I think that is the best solution when you are exposing the superclass to other users in other libraries. If it's only used internally by yourself, I'll use the private/non-default-named generative constructor in the superclass.

这篇关于如何在包含工厂方法时访问抽象超类实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆