据我所知,FunctionalInterface是一种契约,它确保传递给另一个方法的方法的参数和返回类型是正确的类型。例如,
@FunctionalInterface
private interface myFunctionType<R> {
R execute() throws Exception;
}
允许你编写一个函数:
private <T> T methodCaller(final myFunctionType<T> operation) {
return operation.execute();
}
其中&#34;操作&#34;可以是任何带0参数的函数并返回类型为T的对象。有没有办法写&#34; methodCaller&#34;这样&#34;操作&#34;可以是任何带有任何参数的函数(但仍返回T类型的对象)?如果没有,为什么?
基本上类似于:
public T createAnimal(int age) {}
public T createLamp(String style, boolean secondHand) {}
methodCaller(() -> createAnimal(5));
methodCaller(() -> createLamp("plain", true));
但它适用于任何带有ANY参数的函数,它仍会返回T.