在 Java 中,给定一个非静态方法引用和一个类的实例,是否可以通过某种方式传达您希望该类实例调用该方法?
伪例子:
public Object methodApply(Function<Object,Object> method, Object instance, List<Object> args) {
return instance.method(args);
}
最佳答案
你不能声明一个方法来调用any类声明的对象的方法,但是如果你使用reflection .
反射方法的缺点是如果方法/访问无效,您可能会在运行时遇到异常。
使用 Java 8,您可以传递任何 method reference但仅实现功能接口(interface)(它只是 lambda 表达式的替代品)。
限制是这个方法引用必须符合功能接口(interface)描述符。
如果您真的想使用 Java 8 并从类型安全中获益来实现您的需求,您应该利用可以在不属于 lambda 的变量上调用方法引用的优势。
但还要注意,函数式接口(interface)接受的参数数量不是可变的。
因此,您应该重载您的通用方法以接受不同数量的参数。
例如,如果您想将一个参数传递给调用的方法,methodApply()
应该有一个 Function<T,R>
参数:
public static <T, R> R methodApply(Function<T, R> function, T arg) {
return function.apply(arg);
}
但是,如果您还希望能够将两个参数传递给调用的方法,methodApply()
应该重载并且有一个BiFunction<T, U, R>
参数:
public static <T, U, R> R methodApply(BiFunction<T, U, R> function, T argOne, U argTwo) {
return function.apply(argOne, argTwo);
}
如果你需要传递更多的参数,你当然可以创建自己的功能接口(interface):TriFunction
, QuadriFunction
并以相同的方式重载该方法。
这是一个示例工作代码,说明了这一点:
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
public class FunctionHelper {
public static void main(String[] args) {
System.out.println("methodApply() with Function" + System.lineSeparator());
// String.concat
String oneString = "hello";
String concatenated = methodApply(oneString::concat, " world");
System.out.println("concatenated="+ concatenated);
// List add/remove
List<String> list = new ArrayList<>();
methodApply(list::add, concatenated);
System.out.println("after adding, list="+list);
methodApply(list::remove, concatenated);
System.out.println("afer removing, list="+list);
System.out.println("---------------------------"+ System.lineSeparator());
System.out.println("methodApply() with BiFunction" + System.lineSeparator());
// String.substring
oneString = "hello world";
System.out.println("substring=" + methodApply(oneString::substring, 0, 6));
}
public static <T, R> R methodApply(Function<T, R> function, T arg) {
return function.apply(arg);
}
public static <T, U, R> R methodApply(BiFunction<T, U, R> function, T argOne, U argTwo) {
return function.apply(argOne, argTwo);
}
}
输出:
methodApply() with Function
concatenated=hello world
after adding, list=[hello world]
afer removing, list=[]
methodApply() with BiFunction
substring=hello
关于java - 在实例参数上调用实例方法引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46246000/