10 lines of code to understand anonymous inner classes & lambda expressions & functional interfaces

10 lines of code to understand anonymous inner classes & lambda expressions & functional interfaces

10 lines of code to understand anonymous inner classes & lambda expressions & functional interfaces

1. First summary:

For details, see the comments in the detailed code.

1. Anonymous inner class syntax:

new  (){
    @Override
         ( ...){
         
    }
}
 

2. Lambda expression syntax:

( ...) -> { }
 

3. Functional interface:

  1. Interface marked with @FunctionalInterface
  2. Can only be marked on an interface that only defines a single abstract method
  3. The default method and static method in the interface are not considered abstract methods

2. Simplify the code:

class Test {
    public static void main(String[] args) {
        Outer.method().show();
        Outer.method2().show();
    }
}

interface Inner {
    void show();
}

class Outer {
    public static Inner method() {
        return new Inner() {
            @Override
            public void show() {
                System.out.println("Hello Anonymous");
            }
        };
    }

    public static Inner method2() {
        return () -> System.out.println("Hello Lambda");
    }
}
 

3. Detailed code:

/**
 * description:   & lambda   &  
 *
 * @author xiaosaguo
 */
public class AnonymousClassesTest {
    public static void main(String[] args) {
        Outer.method().show();
        Outer.method().show3();
        Inner.show4();

        new Outer().method2().show();
        new Outer().method3().show();
    }
}


@FunctionalInterface
interface Inner {

    /*  
     * 1.   @FunctionalInterface  
     * 2.  
     * 3.   default  
     */

    void show();

    //void show2();

    default void show3() {
        System.out.println("show3");
    }

    static void show4() {
        System.out.println("show4");
    }
}


class Outer {
    private int num0 = 0;
    public static int num1 = 10;
    private final int num2 = 20;
    public static final int num3 = 30;

    private int num11 = 11;

    public static Inner method() {
        /*  
         * 1.  
         * 2.  
         * 3.  
         *      new  (){
         *          @Override
         *               ( ...){
         *               
         *          }
         *      }
         * 4.    
         * 5.   final  
         *       
         *        final  
         * 6.  
         * 7.  
         * 8.  .this  
         */
        int num4 = 40;
        return new Inner() {
            int num5 = 50;

            @Override
            public void show() {
                int num6 = 60;
                System.out.println("HelloWorld" + num1 + num3 + num4 + num5 + num6);
            }

            ///@Override
            ///public void show2() {
            ///    System.out.println("HelloWorld2");
            ///}
        };
    }

    public Inner method2() {
        /* lambda  
         * 1.   lambda  
         * 2.  
         *          ( ...) -> { }
         *       
         *          () -> System.out.println("");
         *        1  :  1 1 
         *          v -> System.out.println(v);
         *       
         *          (k,v) -> System.out.println(k+v);
         *       
         *          () -> return "";
         *       
         *          (k,v) -> {
         *              System.out.println(k+v);
         *              return k + v;
         *          }
         * 3.  
         *       
         * 4. this  lambda   this  this   this
         *        method2   method3
         */
        int num2 = -2;
        return () -> {
            //int num2 = 22;
            System.out.println("HelloWorld" + num0 + num1 + num2 + num3 + this.num2 + Outer.this.num2 + num11);
        };
    }

    public Inner method3() {
        int num7 = 70;
        int num2 = -2;//TODO   num2  
        return new Inner() {
            int num2 = 22;//this.num2

            @Override
            public void show() {
                int num2 = 99;//num2
                System.out.println("HelloWorld" + num0 + num1 + num2 + num3 + this.num2 + Outer.this.num2 + num11 + num7);
            }
        };
    }
}