Java Anonymous Internal Class and Lambda Expression

Keywords: Java Back-end

1. Java Anonymous Internal Class

There are several important points to remember when using anonymous internal classes:
(1) Anonymous inner classes cannot have constructors. (Because anonymous inner classes have no class names)
(2) Anonymous internal classes cannot define any static members, methods, and classes.
(3) An anonymous internal class cannot be an abstract class. (Because when an anonymous internal class is created, an object of the anonymous internal class is created immediately. It is also known that an anonymous internal class must implement its abstract parent or all the abstract methods contained in the interface.)
(4) Anonymous internal classes cannot be public,protected,private,static.
Only one instance of an anonymous internal class can be created.
_An anonymous internal class must be behind new, implying an interface or implementing a class.
Since anonymous inner classes are local inner classes, all restrictions on local inner classes apply to them.
_Internal classes can only access static variables or static methods of external classes.
_When an anonymous internal class accesses a local variable, it is modified with final (which is added automatically by the compiler after JDK1.7)
this in anonymous and internal classes:
_When we use internal classes and anonymous classes, when we use this in anonymous classes,
This this refers to the anonymous class or the internal class itself. If we want to use the methods and variables of the external class, we should add the class name of the external class.


Interface:
(1) Used to define standards (specifications)
(2) Constant, no ordinary variable
3. There is no construction method.
(4) There is no common method
Member method:
1. Abstract methods
2. Static static methods
3. General methods of modifying with default
Interface Multiple Inheritance
Class is single inheritance

2. JavaLambda Expression

Lambda expressions are a new feature of JDK8 that can replace most anonymous internal classes and write more elegant Java code, especially when traversing collections and other collection operations, which can greatly optimize the code structure.
_JDK also provides a large number of built-in functional interfaces for us to use, making the use of Lambda expressions more convenient and efficient.
The syntax form of_is () -> {}, where () is used to describe the parameter list, {} is used to describe the method body, -> is the lambda operator, read as (go to).

In the main function:

Full code:

package homework09.homework0910;
/**
 * Anonymous Inner Class
 * Java Lambda Expression
 */
public class Demo {
    public static void main(String[] args) {

        final int x=120;
        Zoo z=new Zoo() {//This is an anonymous internal class
            @Override
            public void test() {
                System.out.println("This is rewritten test Method!");
                System.out.println(x);
            }
            public void run(){
                System.out.println("This is your own method in an anonymous internal class!");
            }
        };
        z.test();//Call the overridden method
       /* z.run();*///Direct error, if only your own run method in an anonymous internal class can be called, and vice versa


        //Here's how to create objects of an internal class by first new ing the main class, then creating objects of the internal class
        Student.Student2 student = new Student().new Student2();
        student.test9();
        Student.Student1 s = new Student().new Student1();
        s.test3();

        //Override abstract methods in interface Animal, anonymous classes implement Animal interfaces
        //()->{} This anonymous method overrides the abstract method test in Animal
        Animal i=()->{//Java Lambda expression
            System.out.println("This is an override class Animal Abstraction in(abstract)Method");
        };//Note: Don't miss the semicolon
        i.test();//Call the overridden test method

       /* Dog d = ()->{
        //Abstract classes cannot use Lambda expressions to directly error
        };*/

        //1. Multiple parameters are not returned
        Animal1 a1 = (int a,int b)->{
            System.out.println(a+b);
        };
        a1.sum(4, 5);

        //2. Multiple parameters have returns
        Animal2 a2=(a,b)->{
            return a+b;
        };
        int a2sum=a2.sum(4, 9);
        System.out.println(a2sum);

        //3. Braces can be omitted when there is only one line of code in the body of the method
        Animal3 a3 = k->System.out.println(k);
        a3.sum(6);

        //4. The return keyword can be omitted when there is only one line of code in the body of the method with a return value
        Animal4 a4 = k->k;
        int a4sum=a4.sum(9);
        System.out.println(a4sum);

        //5. When the method has no parameters, parentheses cannot be saved
        Animal5 a5 = ()->11;
        int a5sum=a5.sum();
        System.out.println(a5sum);
    }
}
interface Animal5{
    public int sum();
}
interface Animal4{
    public int sum(int a);
}
interface Animal3{
    public void sum(int a);
}
interface Animal2{
    public int sum(int a,int b);
}
interface Animal1{
    public void sum(int a,int b);
}
abstract class Dog{
    public abstract void test();
}
interface Animal{
    //Abstract method
    public abstract void test();
    //Not available until JDK1.8
    public default void test3(){
        System.out.println("test3");
    }
}
abstract class Zoo{//An abstract class is defined
    public abstract void test();//An abstract method is defined
}
class Student{//Members in Classes-->Member Variables-->Member Methods-->Member Classes
    public static final int A=100;//public static final
    int a=20;
    int b=50;//The compiler automatically adds the final keyword to the variable b so that the value of the member variable B can be output in the internal class
    public static void test(){
    }
    public class Student1{
        //All access modifiers for member internal classes can be used
        public void test3(){
            System.out.println(b);
        }
    }
    class Student2{
        int a=10;
        public void test9(){
            System.out.println("The member variable values in the internal class are:"+a);
            System.out.println(A);
            //When an internal class calls a member variable of an external class, it is distinguished by the class name of the external class. this. member variable name
            System.out.println("The values of member variables in the main class are:"+Student.this.a);
        }
    }
}

Code run results:

Posted by mdj on Fri, 10 Sep 2021 10:06:55 -0700