首页 > 其他 > 详细

lambda表达式-匿名内部类

时间:2020-03-02 17:41:44      阅读:64      评论:0      收藏:0      [点我收藏+]

jdk1.8之前:

public class TestTow {
    public static void main(String args[]) {

        TestTow testTow = new TestTow();

                //匿名内部类
        int temp = new MathOperation() {
            @Override
            public int operation(int a, int b) {
                return a + b;
            }
        }.operation(2, 2);

        System.out.print(temp);

    }

         //接口    
    interface MathOperation {
        int operation(int a, int b);
    }

}
   

jdk1.8之后

public class TestTow {
    public static void main(String args[]) {

        TestTow testTow = new TestTow();

        MathOperation mathOperation = (a, b) -> a + b;//用lambda表达式
        int temp = mathOperation.operation(4, 4);//调用方法,并传值
        System.out.print(temp);

    }

    interface MathOperation {
        int operation(int a, int b);
    }

}

 

lambda表达式-匿名内部类

原文:https://www.cnblogs.com/zxrxzw/p/12395974.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!