需求,定义⼀个可以使⽤加减乘除的接⼝ 以前需要定义4个⽅法
一个简单的接口类——小例子
@FunctionalInterface public interface OperFunction<R,T> { R operator(T t1, T t2); }
public class Main { public static void main(String[] args) throws Exception { System.out.println(operator(20, 5, (Integer x, Integer y) -> { return x * y; })); System.out.println(operator(20, 5, (x, y) -> x + y)); System.out.println(operator(20, 5, (x, y) -> x - y)); System.out.println(operator(20, 5, (x, y) -> x / y)); } public static Integer operator(Integer x, Integer y, OperFunction<Integer, Integer> of) { return of.operator(x, y); } }
本文作者为DBC,转载请注明。