JAVA Lambda表达式的使用

package lambda;

public interface Addable {
     int add(int x,int y);
}
package lambda;

public class AddableDemo {
    public static void main(String[] args) {
        //用匿名内部类方式
        useAddable(new Addable() {
            @Override
            public int add(int x, int y) {
                System.out.println(x+"||"+y);
                return 0;
            }
        });
        //用Lambda
        useAddable((int x,int y)->{
            System.out.println(x+"||"+y);
            return 0;
        });
    }
    public static void useAddable(Addable a){
        System.out.println(a.add(1,2));

    }
}

 

posted @ 2022-04-18 21:07  phpwyl  阅读(25)  评论(0编辑  收藏  举报