主要还是看到同事用了,要学习下。
public class Test {
public static void main(String[] args) {
Function<Integer, Integer> fun = x -> x + 1;
Integer integer = TestUtil.execute(2, fun);
System.out.println(integer);
}
}
public class TestUtil<T, R> {
private final Function<T, R> mission;
private final T threshold;
public TestUtil(Function<T, R> mission, T threshold) {
this.mission = mission;
this.threshold = threshold;
}
public static <T, R> R execute(T threshold, Function<T, R>mission) {
return new TestUtil<T, R>(mission, threshold).execute();
}
private R execute() {
return mission.apply(threshold);
}
}
大概就是T是入参类型,R是返回类型。然后先定义方法,在传入apply(参数)。理解起来不难,还是实际中考虑到如何复用