如何实现下面的功能
public T myFunction(Function<T> func) //T is the return type
{
//...some code
return func();
}
我可以使用下面的函数,不需要在myFunction中声明参数a、b、c
myType result = myFunction(() -> doSomething(a, b , c))
如何实现下面的功能
public T myFunction(Function<T> func) //T is the return type
{
//...some code
return func();
}
我可以使用下面的函数,不需要在myFunction中声明参数a、b、c
myType result = myFunction(() -> doSomething(a, b , c))
正确的功能接口是Supplier<T>
.它没有"输入"类型参数:
public <T> T myFunction(Supplier<T> func) {
// myFunction logic
return func.get();
}
调用将与您的调用一样,假设doSomething()
的返回类型为myType
.