lambda表达式-匿名内部类

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);
    }

}

猜你喜欢

转载自www.cnblogs.com/zxrxzw/p/12395974.html