Java 等价于 C# 扩展方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4359979/
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
Java equivalent to C# extension methods
提问by Fabio Milheiro
I am looking to implement a functionality in a list of object as I would in C# using an extension method.
我希望在对象列表中实现一个功能,就像我在 C# 中使用扩展方法一样。
Something like this:
像这样的东西:
List<DataObject> list;
// ... List initialization.
list.getData(id);
How do I do that in Java?
我如何在 Java 中做到这一点?
采纳答案by SLaks
Java does not support extension methods.
Java 不支持扩展方法。
Instead, you can make a regular static method, or write your own class.
相反,您可以创建一个常规的静态方法,或者编写您自己的类。
回答by AlexR
Java does not have such feature. Instead you can either create regular subclass of your list implementation or create anonymous inner class:
Java没有这样的功能。相反,您可以创建列表实现的常规子类或创建匿名内部类:
List<String> list = new ArrayList<String>() {
public String getData() {
return ""; // add your implementation here.
}
};
The problem is to call this method. You can do it "in place":
问题是调用这个方法。你可以“就地”做到这一点:
new ArrayList<String>() {
public String getData() {
return ""; // add your implementation here.
}
}.getData();
回答by J?rg W Mittag
It looks like there is some small chance that Defender Methods (i.e. default methods) might make it into Java 8. However, as far as I understand them, they only allow the authorof an interface
to retroactively extend it, not arbitrary users.
它看起来像有一些小的机会,后卫算法(即默认的方法)可能会使它为Java 8。但是,据我理解他们,他们只允许作者的interface
追溯扩展它,不是任意用户。
Defender Methods + Interface Injection would then be able to fully implement C#-style extension methods, but AFAICS, Interface Injection isn't even on the Java 8 road-map yet.
Defender Methods + Interface Injection 将能够完全实现 C# 风格的扩展方法,但 AFAICS,Interface Injection 甚至不在 Java 8 路线图上。
回答by Aravind Yarram
Another option is to use ForwardingXXXclasses from google-guava library.
另一种选择是使用google-guava 库中的ForwardingXXX类。
回答by magritte
Bit late to the party on this question, but in case anyone finds it useful I just created a subclass:
关于这个问题的聚会有点晚了,但如果有人觉得它有用,我刚刚创建了一个子类:
public class ArrayList2<T> extends ArrayList<T>
{
private static final long serialVersionUID = 1L;
public T getLast()
{
if (this.isEmpty())
{
return null;
}
else
{
return this.get(this.size() - 1);
}
}
}
回答by DarVar
Java
8 now supports default methods, which are similar to C#
's extension methods.
Java
8 现在支持默认方法,类似于C#
的扩展方法。
回答by Sam Keays
回答by user1686250
Extension methods are not just static method and not just convenience syntax sugar, in fact they are quite powerful tool. The main thing there is ability to override different methods based on different generic's parameters instantiation. This is similar to Haskell's type classes, and in fact, it looks like they are in C# to support C#'s Monads (i.e. LINQ). Even dropping LINQ syntax, I still don't know any way to implement similar interfaces in Java.
扩展方法不仅仅是静态方法,也不仅仅是方便的语法糖,事实上它们是非常强大的工具。最重要的是能够根据不同的泛型参数实例覆盖不同的方法。这类似于 Haskell 的类型类,实际上看起来它们在 C# 中是为了支持 C# 的 Monads(即 LINQ)。即使放弃了 LINQ 语法,我仍然不知道在 Java 中实现类似接口的任何方法。
And I don't think it is possible to implement them in Java, because of Java's type erasure semantics of generics parameters.
而且我认为不可能在 Java 中实现它们,因为 Java 的泛型参数的类型擦除语义。
回答by Thomas Eizinger
Project Lombokprovides an annotation @ExtensionMethod
that can be used to achieve the functionality you are asking for.
Project Lombok提供了一个注释@ExtensionMethod
,可用于实现您要求的功能。
回答by Eric
One could be use the decorator object-oriented design pattern. An example of this pattern being used in Java's standard library would be the DataOutputStream.
一种是使用装饰器面向对象的设计模式。在 Java 的标准库中使用这种模式的一个例子是DataOutputStream。
Here's some code for augmenting the functionality of a List:
下面是一些用于增强 List 功能的代码:
public class ListDecorator<E> implements List<E>
{
public final List<E> wrapee;
public ListDecorator(List<E> wrapee)
{
this.wrapee = wrapee;
}
// implementation of all the list's methods here...
public <R> ListDecorator<R> map(Transform<E,R> transformer)
{
ArrayList<R> result = new ArrayList<R>(size());
for (E element : this)
{
R transformed = transformer.transform(element);
result.add(transformed);
}
return new ListDecorator<R>(result);
}
}
P.S. I'm a big fan of Kotlin. It has extension methods and also runs on the JVM.
PS 我是Kotlin 的忠实粉丝。它有扩展方法,也可以在 JVM 上运行。