Internal class -- java self-study

Basic introduction

Another class structure is completely nested inside a class. The nested class is called the inner class, and the class nested with other classes is called the outer class. The biggest feature of the inner class is that it can directly access private properties and reflect the inclusion relationship between classes
Basic grammar

class Outer{ //External class
          class Inner{ //Inner class              
          }
}
class Other{ //External other classes
}
public class InnerClass01 {//External other classes
    public static void main(String[] args) {
        
    }
}
class Outer{//External class
    private int n1 = 100;//attribute

    public Outer(int n1) {//constructor 
        this.n1 = n1;
    }
    public void m1(){//method
        System.out.println("method");
    }
    {//Code block
        System.out.println("Code block");
    }
    class Iner{//Inner class, inside Outer
        
    }
}

Classification of internal classes

1. Defined at a local location of an external class (such as within a method):
a. Local inner class (with class name)
b. Anonymous inner class (no class name) key
2. Defined on the member position of the external class:
a. Member inner class (not decorated with static)
b. Static inner class (decorated with static)

Local inner class

The local inner class is defined in the local position of the outer class, such as in the method, and has a class name
1. Local internal classes can directly access all members of external classes, including private ones
2. The local internal class cannot add an access modifier because its status is a local variable. The modifier cannot be used for local variables, but it can use the final modifier because local variables can also use final
3. The scope of the local inner class is only in the method or code block that defines it
4. The local inner class accesses the members of the external class directly
5. Members of external classes accessing local internal classes must be within the scope
Access method: create an object and then access it

//Demonstrates the use of local inner classes
public class LocalInnerClass {
    public static void main(String[] args) {
        Outer2 outer2 = new Outer2();
        outer2.m1();
    }
}
class Outer2{
    private int n1 =100;
    public void m1(){//method
        //1. The local inner class is defined in the local position of the external class, usually in the method
        //3. The access modifier cannot be added, but the final modifier can be used
        //final class Inner02{}
        //4. The local internal class scope is only in the method or code block that defines it. Inner02 can only be used in m1
        class Inner02{//Local inner class
            //2. You can directly access all members of external classes, including private ones
            public void f1(){
                //5. The local internal class directly accesses the members of the external class
                System.out.println(n1);
            }
        }

        class Inner03 extends Inner02{}
        //6. The external class accesses the members of the local internal class
        //Access method: create an object and then access it
        Inner02 inner02 = new Inner02();
        inner02.f1();
    }
    {//Code block
        class Inner03{}
    }
    }
remember:
(1)Local inner classes are defined in methods/Code block
(2)The scope is in the method body or code block
(3)Nature is class

6. Other external classes cannot access local internal classes
7. If the members of the external class and the local internal class have the same name, the proximity principle is followed by default. If you want to access the members of the external class, you can use (external class name. this. Member)

Anonymous Inner Class

An anonymous inner class is defined in a local location of an outer class, such as a method, and does not use a class name
(1) The essence is a class (2) an inner class (3) the class has no name (4) and is also an object

Basic grammar
new Class or interface(parameter list){
     Class body
};
public class AnonymousInnerClass {
    public static void main(String[] args) {
        Outer04 outer04 = new Outer04();
        outer04.method();
    }
}

class Outer04{//External class
    private int n1 = 10;//attribute
    public void method(){//method
       //Interface based anonymous inner class
       //1. The requirement wants to use interface A and create an object
       //2. The traditional way is to create a class to implement the interface and create an object
       //3. The requirement class is only used once and will not be used later
       //4. Use anonymous inner classes to simplify
       //5. The compilation type of tiger is A
       //6. The running type of tiger is an anonymous internal class (the name of the external class running type is + $1, which is allocated by the system and released once used)
       /*
          bottom
          class XXXX implements IA{
           @Override
           public void cry(){
               System.out.println("The tiger calls ");
           }
          }
       */
       //7. When the JDK bottom layer creates the anonymous inner class Outer04 , it immediately creates an instance of Outer04  and sends the address
       //Back to tiger
       //8. Once used, the anonymous inner class can no longer be used. tiger objects can be used repeatedly
       A Tiger = new A(){
           @Override
           public void cry(){
               System.out.println("Tiger barking......");
           }
       };
       Tiger.cry();

        //Demonstrates anonymous inner classes based on classes
        //1. The compilation type of Father is Father
        //2. The operation type of father is Outer04
        //3. The bottom layer will create anonymous inner classes
        /*
        class Outer04$2 extends  Father{
        }
        */
        //4. The anonymous inner class Outer04  object is also returned directly
        Father father = new Father("jack"){

        };
    }
}
interface A{//Interface
    public void cry();
}
class Father{//class
    String name;
    public Father(String name){
            this.name = name;
    }
    public void test(){//method

    }
}
Anonymous inner class details

1. Anonymous inner classes are both classes and objects

Call method 1
Person p = new Person(){
public void hi(){}
};

Call method 2 if it is used only once
new Person(){
public void hi(){}
}.hi();

2. You can directly access all members of external classes, including private ones
3. Access modifier cannot be added
4. The scope is only in the method or code block that redefines it
5. Anonymous internal classes access external members, and direct access
6. Other external classes cannot access anonymous internal classes
7. If the members of the external class and the anonymous internal class have the same name, the anonymous internal class access follows the proximity principle. If you want to access the members of the external class, you can use (external class class name. this. Member) to access them

Anonymous inner class practice

It is passed directly as an argument, which is concise and effective

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

        //Pass directly as an argument
        f1(new IL(){
            public void show(){
                System.out.println("This is a famous painting");
            }
        });
    }

    //Static method, formal parameter is interface type
    public static void f1(IL il){
        il.show();
    }
}
interface IL{//Interface
    void show();
}

Keywords: Java Class

Added by jtgraphic on Mon, 03 Jan 2022 12:00:30 +0200