lambda表达式

public class Day07 {
    public static void main(String[] args) {
//        I i = n -> {
//            int m = 24;
//            int ru = m * n;
//            return ru;
//        };
//        System.out.println(i.say(3));

//        I i1 = () -> new Day07().m1();
//        i1.say();

//        I<Integer> i = (m) -> {
//            int n = 2;
//            int num = n * m;
//            return num;
//        };
//        System.out.println(i.say(34));

//        //局部变量l相对于lambda函数内部为final类型的常量,只能访问,不可修改
//        int  l = 10;
//        I i = () -> {
//            int a = 10;
//            l = l + a;
//        };
//

//        I i = Day07::m1;  //等价于 ()->Day07.m1()
//        i.say();

        //class::function ::称为方法引用,实际是对传入的数据直接调用class的function方法进行处理
        //如:list.forEach(System.out::println) 调用System.out的println方法对forEach遍历到的元素打印输出
        I i = n -> n;
        I i1 = Day07::m1;    //等价于 (s)-> Day07.m2(s),这里已完成对接口I的say方法的实现(运用时必须保证接口只有一个未实现的方法)
        System.out.println(i1.say("ss"));

    }

    private static String m2(String s) {
        return s;
    }

    public static String m1(String a) {
        System.out.println("m1");
        return a;
    }

}

interface I {
    public String say(String s);

    public static String sa() {
        return "qa";
    }
}

 

2、关于::

class::function ::称为方法引用,实际是对传入的数据直接调用class的function方法进行处理



 


 



 


 


 

 
posted @ 2022-04-24 16:08  guhezhu  阅读(33)  评论(0)    收藏  举报