在 Java 中实现 Mixin?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/587458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implement Mixin In Java?
提问by
回答by Johannes Weiss
Since Java only supports single inheritance, that is not possible. Have a look at WP: Mixin.
由于 Java 只支持单继承,这是不可能的。看看WP: Mixin。
EDIT: Because of the comments about interfaces: The cool thing about mixins is that you can combine them without writing the combination's code. With interfaces you have to implement the combination's functionality yourself (except one class you can extend)!
编辑:因为关于接口的评论:关于 mixin 很酷的事情是你可以在不编写组合代码的情况下组合它们。对于接口,您必须自己实现组合的功能(除了一个可以扩展的类)!
回答by TofuBeer
Faking mixins in Java: http://jonaquino.blogspot.com/2005/07/java-mixin-pattern-or-faking-multiple.html
在 Java 中伪造 mixins:http: //jonaquino.blogspot.com/2005/07/java-mixin-pattern-or-faking-multiple.html
回答by Eddie
In the sense that a Ruby mix-in is the equivalent of a Java abstract class, no, you cannot implement a mix-in in Java. You can come close by using interfaces and thus defining absolutely no code in your mix-in, but you cannot directly achieve the same behavior as in a Ruby mix-in.
从某种意义上说,Ruby 混入相当于 Java 抽象类,不,您不能在 Java 中实现混入。您可以通过使用接口来接近,从而在您的混入中绝对不定义任何代码,但您不能直接实现与 Ruby 混入中相同的行为。
回答by Scott Stanchfield
Take a peek at http://code.google.com/p/javadude/wiki/AnnotationsMixinExample
看一看http://code.google.com/p/javadude/wiki/AnnotationsMixinExample
It's using a set of annotations I've created.
它使用了我创建的一组注释。
Note: I'm working on a major update to the annotations, which includes some API breakage. I plan to release a new version in the next few weeks.
注意:我正在对注释进行重大更新,其中包括一些 API 损坏。我计划在接下来的几周内发布一个新版本。
回答by Martin
I'd say just use object composition. Every time you want to throw in new functionality, compose another object into the class as a member. If you want to make all of your mixed-in classes of the same type, you can use an array as a member object where each element is composed with all of the others, and you can dispatch to a particular element.
我会说只是使用对象组合。每次你想加入新功能时,将另一个对象作为成员组合到类中。如果要使所有混合类都具有相同类型,则可以使用数组作为成员对象,其中每个元素都由所有其他元素组成,并且可以分派到特定元素。
回答by leeand00
I believe thismay answer you question...although I'm not completely sure I understand what a mixin is yet...
我相信这可以回答你的问题......虽然我不完全确定我理解什么是mixin......
回答by Daniel Fanjul
You could use CGLIBfor that. The class Mixinis able to generate a dynamic class from several interfaces / object delegates:
您可以为此使用CGLIB。Mixin类能够从多个接口/对象委托生成动态类:
static Mixin create(java.lang.Class[] interfaces,
java.lang.Object[] delegates)
static Mixin create(java.lang.Object[] delegates)
static Mixin createBean(java.lang.Object[] beans)
回答by Brad Cupit
The simplest approach is to use static imports. It allows for code reuse that 'looks' like it's part of the class, but is really defined elsewhere.
最简单的方法是使用静态导入。它允许代码重用,“看起来”它是类的一部分,但实际上是在其他地方定义的。
Pros:
优点:
- really easy
- you can 'mixin' as many static imports as you like
- 真的很容易
- 您可以根据需要“混合”任意数量的静态导入
Cons:
缺点:
- the static methods won't have access to 'this', so you'd have to pass it in manually
- no state: your static methods can't have their own instance fields. They can only define their own static fields, which are then shared by any object calling the static method.
- can't define public methods on the client class (the one with code being mixed into it). In Ruby, importing a mixin will actually define those public methods as public methods on your class. In Java, inheritance would be a better solution in this case (assuming you don't need to extend multiple classes)
- 静态方法无法访问“this”,因此您必须手动传递它
- 无状态:您的静态方法不能有自己的实例字段。它们只能定义自己的静态字段,然后由调用静态方法的任何对象共享。
- 不能在客户端类上定义公共方法(其中混入了代码的那个)。在 Ruby 中,导入 mixin 实际上会将这些公共方法定义为类中的公共方法。在 Java 中,在这种情况下继承将是更好的解决方案(假设您不需要扩展多个类)
Example:
例子:
import static my.package.MyHelperUtility.methodDefinedInAnotherClass;
public class MyNormalCode {
public void example() {
methodDefinedInAnotherClass();
}
}
回答by Ray Tayek
just ran across: https://blog.berniesumption.com/software/mixins-for-java/(Borken link updated)
刚刚遇到:https://blog.berniesumption.com/software/mixins-for-java/(Borken 链接已更新)
回答by SergeZ
Yes, the most simple and convinient way to implement mixins apporoach in Java - is to use static import from some class which contains static methods.
是的,在 Java 中实现 mixins 方法的最简单和方便的方法是使用来自某个包含静态方法的类的静态导入。