函数式编程

新建函数接口

Operation

@FunctionalInterface
public interface Operation<R, T> {

    R operator (T t1, T t2);
}

在测试类中实现

public class TestDemo {

    @Test
    public void testOperator() {
        System.out.println(operator(2, 5, (x, y) -> {
            return x * y;
        }));
    }

    private static Integer operator(Integer x, Integer y,
                                    Operation<Integer, Integer> operation) {

        return operation.operator(x, y);
    }
}

结果

image

posted @ 2022-10-03 21:14  jarico  阅读(22)  评论(0)    收藏  举报