我希望在对象列表中实现一个功能,因为我会在c#中使用扩展方法。
就像这样:
List<DataObject> list;
// ... List initialization.
list.getData(id);
在Java中怎么做呢?
我希望在对象列表中实现一个功能,因为我会在c#中使用扩展方法。
就像这样:
List<DataObject> list;
// ... List initialization.
list.getData(id);
在Java中怎么做呢?
当前回答
在Java中没有扩展方法,但你可以通过以下方式来获得它:
你通过下面的例子定义字符串的“echo”方法,
@Extension
public class MyStringExtension {
public static void echo(@This String thiz) {
System.out.println(thiz);
}
}
在那之后,你可以在任何地方对字符串使用这个方法(echo),
"Hello World".echo(); //prints "Hello World"
"Welcome".echo(); //prints "Welcome"
String name = "Jonn";
name.echo(); //prints "John"
当然,你也可以有这样的参数,
@Extension
public class MyStringExtension {
public static void echo(@This String thiz, String suffix) {
System.out.println(thiz + " " + suffix);
}
}
像这样使用,
"Hello World".echo("programming"); //prints "Hello World programming"
"Welcome".echo("2021"); //prints "Welcome 2021"
String name = "John";
name.echo("Conor"); //prints "John Conor"
你也可以看看这个样品,歧管样品
其他回答
你可以通过(RE)实现Collections接口来创建一个类似c#的扩展/助手方法,并添加Java Collection的示例:
public class RockCollection<T extends Comparable<T>> implements Collection<T> {
private Collection<T> _list = new ArrayList<T>();
//###########Custom extension methods###########
public T doSomething() {
//do some stuff
return _list
}
//proper examples
public T find(Predicate<T> predicate) {
return _list.stream()
.filter(predicate)
.findFirst()
.get();
}
public List<T> findAll(Predicate<T> predicate) {
return _list.stream()
.filter(predicate)
.collect(Collectors.<T>toList());
}
public String join(String joiner) {
StringBuilder aggregate = new StringBuilder("");
_list.forEach( item ->
aggregate.append(item.toString() + joiner)
);
return aggregate.toString().substring(0, aggregate.length() - 1);
}
public List<T> reverse() {
List<T> listToReverse = (List<T>)_list;
Collections.reverse(listToReverse);
return listToReverse;
}
public List<T> sort(Comparator<T> sortComparer) {
List<T> listToReverse = (List<T>)_list;
Collections.sort(listToReverse, sortComparer);
return listToReverse;
}
public int sum() {
List<T> list = (List<T>)_list;
int total = 0;
for (T aList : list) {
total += Integer.parseInt(aList.toString());
}
return total;
}
public List<T> minus(RockCollection<T> listToMinus) {
List<T> list = (List<T>)_list;
int total = 0;
listToMinus.forEach(list::remove);
return list;
}
public Double average() {
List<T> list = (List<T>)_list;
Double total = 0.0;
for (T aList : list) {
total += Double.parseDouble(aList.toString());
}
return total / list.size();
}
public T first() {
return _list.stream().findFirst().get();
//.collect(Collectors.<T>toList());
}
public T last() {
List<T> list = (List<T>)_list;
return list.get(_list.size() - 1);
}
//##############################################
//Re-implement existing methods
@Override
public int size() {
return _list.size();
}
@Override
public boolean isEmpty() {
return _list == null || _list.size() == 0;
}
XTend语言(它是Java的超级集,可编译为Java源代码1)支持这一点。
在Java中没有扩展方法,但你可以通过以下方式来获得它:
你通过下面的例子定义字符串的“echo”方法,
@Extension
public class MyStringExtension {
public static void echo(@This String thiz) {
System.out.println(thiz);
}
}
在那之后,你可以在任何地方对字符串使用这个方法(echo),
"Hello World".echo(); //prints "Hello World"
"Welcome".echo(); //prints "Welcome"
String name = "Jonn";
name.echo(); //prints "John"
当然,你也可以有这样的参数,
@Extension
public class MyStringExtension {
public static void echo(@This String thiz, String suffix) {
System.out.println(thiz + " " + suffix);
}
}
像这样使用,
"Hello World".echo("programming"); //prints "Hello World programming"
"Welcome".echo("2021"); //prints "Welcome 2021"
String name = "John";
name.echo("Conor"); //prints "John Conor"
你也可以看看这个样品,歧管样品
扩展方法不仅仅是静态方法,也不仅仅是方便的语法糖,实际上它们是非常强大的工具。主要的事情是基于不同泛型的参数实例化重写不同方法的能力。这类似于Haskell的类型类,事实上,它们看起来像是在c#中支持c#的单子(即LINQ)。即使放弃LINQ语法,我仍然不知道如何在Java中实现类似的接口。
而且我认为在Java中实现它们是不可能的,因为Java的泛型参数的类型清除语义。
可以使用面向对象的装饰器设计模式。在Java的标准库中使用这种模式的一个例子是DataOutputStream。
下面是一些增强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);
}
}
附注:我是Kotlin的忠实粉丝。它有扩展方法,也运行在JVM上。