java magritte_Java相当于C#扩展方法

Java相当于C#扩展方法

我希望在C#中使用扩展方法实现对象列表中的功能。

像这样的东西:

List list;

// ... List initialization.

list.getData(id);

我如何用Java做到这一点?

12个解决方案

159 votes

Java不支持扩展方法。

相反,您可以创建常规静态方法,或编写自己的类。

SLaks answered 2019-05-08T07:33:15Z

42 votes

扩展方法不仅仅是静态方法,而不仅仅是方便语法糖,实际上它们是非常强大的工具。 主要的是能够根据不同的泛型参数实例化覆盖不同的方法。 这类似于Haskell的类型类,事实上,看起来它们在C#中支持C#的Monads(即LINQ)。 即使删除LINQ语法,我仍然不知道在Java中实现类似接口的任何方法。

而且我认为不可能在Java中实现它们,因为Java的泛型参数的类型擦除语义。

user1686250 answered 2019-05-08T07:33:48Z

13 votes

Project Lombok提供了注释@ExtensionMethod,可用于实现您要求的功能。

Thomas Eizinger answered 2019-05-08T07:34:13Z

7 votes

XTend语言 - 它是Java的超集,并且编译为Java源代码1 - 支持这一点。

Sam Keays answered 2019-05-08T07:34:37Z

6 votes

Java没有这样的功能。相反,您可以创建列表实现的常规子类或创建匿名内部类:

List list = new ArrayList() {

public String getData() {

return ""; // add your implementation here.

}

};

问题是调用此方法。 你可以“到位”做到:

new ArrayList() {

public String getData() {

return ""; // add your implementation here.

}

}.getData();

AlexR answered 2019-05-08T07:35:08Z

6 votes

另一个选择是使用google-guava库中的ForwardingXXX类。

Aravind R. Yarram answered 2019-05-08T07:35:32Z

4 votes

看起来Defender方法(即默认方法)可能会进入Java 8的可能性很小。但是,据我所知,它们只允许interface的作者追溯扩展它,而不是任意用户。

Defender Methods + Interface Injection将能够完全实现C#风格的扩展方法,但AFAICS,Interface Injection甚至还没有在Java 8路线图上。

Jörg W Mittag answered 2019-05-08T07:36:05Z

4 votes

Manifold为Java提供了C#风格的扩展方法和其他一些功能。 与其他工具不同,Manifold没有任何限制,也不会遇到泛型,lambdas,IDE等问题.Manifold提供了其他一些功能,如F#风格的自定义类型,TypeScript风格的结构接口和Javascript风格的expando类型。

此外,IntelliJ通过Manifold插件为Manifold提供全面支持。

Manifold是github上的一个开源项目。

Scott McKinney answered 2019-05-08T07:36:44Z

3 votes

在这个问题上有点迟到了,但是如果有人发现它有用,我只是创建了一个子类:

public class ArrayList2 extends ArrayList

{

private static final long serialVersionUID = 1L;

public T getLast()

{

if (this.isEmpty())

{

return null;

}

else

{

return this.get(this.size() - 1);

}

}

}

magritte answered 2019-05-08T07:37:09Z

2 votes

从技术上讲,C#Extension在Java中没有等价物。 但是,如果您确实希望实现此类函数以实现更清晰的代码和可维护性,则必须使用Manifold框架。

package extensions.java.lang.String;

import manifold.ext.api.*;

@Extension

public class MyStringExtension {

public static void print(@This String thiz) {

System.out.println(thiz);

}

@Extension

public static String lineSeparator() {

return System.lineSeparator();

}

}

M.Laida answered 2019-05-08T07:37:33Z

1 votes

可以使用装饰器面向对象的设计模式。 Java标准库中使用的此模式的一个示例是DataOutputStream。

这是一些用于扩充List功能的代码:

public class ListDecorator implements List

{

public final List wrapee;

public ListDecorator(List wrapee)

{

this.wrapee = wrapee;

}

// implementation of all the list's methods here...

public ListDecorator map(Transform transformer)

{

ArrayList result = new ArrayList(size());

for (E element : this)

{

R transformed = transformer.transform(element);

result.add(transformed);

}

return new ListDecorator(result);

}

}

附: 我是Kotlin的忠实粉丝。 它具有扩展方法,也可以在JVM上运行。

Eric answered 2019-05-08T07:38:12Z

-8 votes

Java 8现在支持默认方法,类似于C#的扩展方法。

DarVar answered 2019-05-08T07:38:37Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值