java方法变量,Lambda表达式

Lambda表达式

public class Test {
    public static void main(String[] args) {
        fun((a) -> a + 100);
    }

    static void fun(Function<Integer, Integer> fun) {
        System.out.println(fun.apply(10));
    }
    @FunctionalInterface
    static interface Function<T, S> {
        S apply(T t);
    }
}

一个参数的Function接口

public class Test {
    public static void main(String[] args) {
        fun((a) -> a + 100);
    }

    static void fun(java.util.function.Function<Integer, Integer> fun) {
        System.out.println(fun.apply(10));
    }
}

两个参数的Function接口

public class Test {
    public static void main(String[] args) {
        fun((a, b) -> a + b + 100);
    }

    static void fun(java.util.function.BiFunction<Integer, Integer, Integer> fun) {
        System.out.println(fun.apply(10, 20));
    }
}
posted @ 2021-05-08 14:25  fight139  阅读(90)  评论(0编辑  收藏  举报