TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Multiple Inheritance and Traits in Java

20 点作者 rlmw超过 13 年前

4 条评论

astral303超过 13 年前
If you are using mulitple inheritance to reuse code, you should think twice (even with regular inheritance). Unless there is a meaningful is-a relationship, this is best accomplished via composition.<p>If you are in the Java world, this is where it pays to switch some classes to Groovy and use its @Delegate annotation. It's a compile-time AST transformation, such that straight-up Java code can take advantage, and it can inject the delegated interfaces into its parent class (so now the class can show up as implementing an interface for the dependency injection framework).<p>This can be introduced on an as-needed basis (make some files Groovy and the Java code can call them) and Groovy is essentially 95% compatible with identical Java code, so you can usually rename .java to .groovy and be good to go.
评论 #3006736 未加载
评论 #3006026 未加载
评论 #3005540 未加载
评论 #3005617 未加载
yock超过 13 年前
Is it bad that the first thing I saw in his example is void methods returning strings and immediately scoffed and closed the window?
quatrevingts超过 13 年前
More evidence that if a language lacks macros, people will find an alternate mechanism for language extension (bytecode generation) which is usually much more complex than macros would have been.<p>On the other hand, reflection in the hands of over-eager users is already a nightmare... perhaps a huge speed bump in the way of full metaprogramming is a good thing for most projects.<p>As far as the actual application presented here.. it doesn't quite get you Scala's traits (no self-types) but it seems pretty close. Nice hack.
rlmw超过 13 年前
I'm always wondering whether HN people think this kind of experimenting is a good idea or not.
评论 #3005515 未加载