提供 Java 特征的库或语言扩展?
我刚刚了解了特征(Smalltalk 中的“特征”,Perl 中的“角色”)。我想用我熟悉的语言快速尝试它们。 Java 中是否有支持特征的库或扩展?
我听说 AspectJ 和 Qi4J 都支持 mixins,但我也了解到 mixins 与特征不同。那么我也可以使用这些库之一来获取特征吗?
或者 Scala 怎么样,它完全向后兼容 Java,对吧?这支持特质吗?
还有其他建议吗?
I've just been introduced to traits ("traits" in Smalltalk, "roles" in Perl). I'd like to experiment with them quickly, in a language I'm familiar with. Are there any libraries or extensions in Java that support traits?
I've heard that AspectJ and Qi4J both support mixins, but I've also read that mixins are different from traits. So can I also use one of those libraries for traits?
Or how about Scala, that's fully backward compatible with Java, right? Does that support traits?
Any other suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,Scala 完全支持 Traits。它必须跳过一些奇怪的环节才能与 JVM 一起工作,但它就在那里,而且非常强大。
您可以在 Scala 中编写实验,它将与您现有的 Java 内容进行互操作(取决于工具)
Yes, Scala has full support for Traits. It has to jump through a few weird hoops to make it work with the JVM, but it's there, and it's very powerful.
You can write your experiment in Scala, and it'll interop with your existing Java stuff (tools depending)
Qi4j 2.0(正在开发中)增加了强大的 Scala 支持,我们正在努力使 Scala Traits 用作 Qi4j mixins 和关注点(AOP“around suggest”等价物)。
Scala 特征编译为静态方法,该方法采用所属类的代理实例作为第一个参数(C 中的经典 C++ 表示形式)。 Qi4j 将能够将 Trait 连接为 Mixin(即方法可以在 Composite 的公共接口中公开)并绑定在底层 StateHolder 中,以便 Trait 使用组合,因此看起来 Trait 具有状态...
FTR; Qi4j 2.0 还最大限度地减少了类型占用空间,从而允许比以前更多地使用与 Qi4j 无关的类型。
Qi4j 2.0 (in the works) adds strong Scala support, and we are working on making Scala Traits useful as Qi4j mixins and concerns (AOP "around advice" equivalents).
The Scala trait compiles down to static methods that takes a proxy instance of the owning class as the first argument (classic C++ representation in C). Qi4j will be able to wire up the the Trait as a Mixin (i.e. the methods can be exposed in the Composite's public interface) and bind in the underlying StateHolder for the composite to be used by the Trait, so it seems that traits will have state...
FTR; Qi4j 2.0 is also minimizing the type footprint, allowing for Qi4j-agnostic types to be used even more than before.