Google Dart 支持 mixin 吗?
我浏览过 语言文档,似乎 Google Dart 不支持 mixins (没有方法接口中的主体,没有多重继承,没有类似 Ruby 的模块)。我的说法是对的吗?还是有其他方法可以在 Dart 中实现类似 mixin 的功能?
I've skimmed through the language documentation and it seems that the Google Dart does not support mixins (no method bodies in interfaces, no multiple inheritance, no Ruby-like modules). Am I right about this, or is there another way to have mixin-like functionality in Dart?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很高兴地告诉大家,现在答案是肯定的!
mixin 实际上只是子类和超类之间的增量。然后,您可以将该增量“混合”到另一个类中。
例如,考虑这个抽象类:
然后您可以将其混合到其他类中,从而避免继承树的污染。
mixin 定义的限制包括:
一些附加阅读:
I'm happy to report that the answer is now Yes!
A mixin is really just the delta between a subclass and a superclass. You can then "mix in" that delta to another class.
For example, consider this abstract class:
You can then mix this into other classes, thus avoiding the pollution of the inheritance tree.
Restrictions on mixin definitions include:
Some additional reading:
编辑:
Dart 团队现在发布了他们的 Mixins 提案< /a>,Mixin 的原始问题在这里。
它尚未实现,但与此同时,我发布了一个可扩展的 Dart Mixins 库,其中包括流行的 Underscore.js 功能实用程序库的端口:https://github.com/mythz/DartMixins
Edit:
The Dart team have now released their proposal for Mixins, the original issue for Mixins was here.
It's not implemented yet, but in the meantime I've released an extensible Mixins library for Dart which includes a port of the popular Underscore.js functional utility library: https://github.com/mythz/DartMixins