我有这段代码:
public class FunctionTest {
public Double getDouble(Function<Integer, Double> function) {
function = t -> Double.valueOf(t);
return function.apply(???); //not sure how to implement this
}
public Function<Integer, Double> getFunction(Integer t) {
return r -> Double.valueOf(t); // not able to fetch value from this as the return type is Function
}
public static void main(String[] args) {
Function<Integer, Double> f = t -> Double.valueOf(t); // works fine
System.out.println(f.apply(4)); // how to aechive this using method?
System.out.println(f);
}
}
如果您看到t -> Double.valueOf(t)
与f.apply(4)
组合合作正常,那么问题是如何设计一个方法,我尝试了两种方法但失败了。
请帮助我。
答案 0 :(得分:0)
对不起,我刚想出怎么做,很简单,但不确定,如果它有效
public Function<Integer, Double> getFunction() {
return r -> Double.valueOf(r);
}
然后
object.getFunction().apply(integer)