Detailed explanation of the use of Java internal classes

One, the relevant introduction of the internal class

1. What is an inner class

One class contains another class, 内部类编译之后是单独.class文件(Format: external class name$internal class name.class).

2. Classification of internal classes

  • Member inner class
  • Local inner class ( 匿名内部类)

Second, the definition and use of internal classes

1. Member inner class

Class inside class

format

权限修饰符 class 外部类名称{
    
    
    权限修饰符 class 内部类名称{
    
    
    
    }
}

Use member inner class

Two methods to access the internal class content of a member

  • In the method of the external class, access the internal class, and main calls the external method
  • Create an internal class object directly, the format of creating an internal class object is as follows
    External class name. Internal class name object name = newexternal class name () . newinternal class name ();
//外部类
class External {
    
    

    //外部类方法
    public void methodExternal(){
    
    
        System.out.println("外部类方法");
        //在外部类方法中创建内部类对象,访问内部类方法
        new Internal().methodInternal();
    }

    //成员内部类
    public class Internal{
    
    
        //内部类方法
        public void methodInternal(){
    
    
            System.out.println("内部类方法");
        }
    }
}

public class Test{
    
    
    public static void main(String[] args) {
    
    
        //借助外部类方法,访问内部类方法
        External external = new External();
        external.methodExternal();
        System.out.println("=========");
        
        //直接创建内部类对象使用内部类方法
        External.Internal internal = new External().new Internal();
        internal.methodInternal();
    }
}

operation result
Insert picture description here
【tips】

The inner class can freely access the contents of the outer class
外部类访问内部类内容时,必须通过内部类对象来访问
(because when there are multiple inner classes at the same time, the contents of different inner classes may be repeated)

2. Local inner class

Local inner class

Inner class inside method

format

public void test(){
    
    
	权限修饰符 class 内部类名称{
    
    
    }
}

public class External {
    
    
    public void methodExternal(){
    
    
    	//外部类成员方法的内部类(有用,代码块后的【tips】会解释)
        [final] int num = 10;
        //在方法内部定义的内部类,叫做局部内部类
        class Internal{
    
    
            public void methodInternal(){
    
    
            	//内部类使用外部类的局部变量
                System.out.println(num);
            }
        }
    }
}

【tips】
Local variables used by inner classes must be guaranteed 变量的值事实不可变.

The reasons are as follows:
1. The new object is in the heap memory
2. The local variables follow the method in the stack memory
3. After the method runs, the stack is immediately popped, and the local variables will disappear immediately
4. But new comes out The object will continue to exist in the heap memory until the garbage collection disappears

Starting from jdk 8, the final keyword can be omitted , but the value of local variables must be guaranteed to be immutable.

Anonymous inner class ( 重要)

Inner class with no name inside the method
Anonymous inner classes can be conveniently used as method parameters or return values
Use scenario analysis

If the implementation class of the interface (or the subclass of the parent class) only needs to be used only once, then the definition format of the class can be omitted in this case, and the anonymous inner class can be used.

format

//大括号内部才属于匿名内部类
接口名称 对象名 = new 接口名称(){
     
     
   //覆盖重写所有抽象方法
}

Analyze the format:
1. new represents the action of creating an object
2. The interface name represents which interface the anonymous inner class needs to implement
3. {...} is the content of the anonymous inner class

interface Interface {
    
    
    void method();
}

public class Demo01Main {
    
    
    public static void main(String[] args) {
    
    
        
        //使用匿名内部类创建对象
        Interface anInterface = new Interface() {
    
    
            @Override
            public void method() {
    
    
                System.out.println("匿名内部类重写了抽象方法");
            }
        };
        //使用匿名内部类对象,调用method方法
        anInterface.method();


        //使用匿名内部类、匿名对象,调用method方法
        new Interface(){
    
    
            @Override
            public void method() {
    
    
                System.out.println("匿名内部类重写了抽象方法,并且使用的匿名对象调用");
            }
        }.method();
    }
}

operation result
Insert picture description here
【tips】

使用一次匿名内部类只能创建一个对象

The difference between anonymous objects and anonymous inner classes

Anonymous object: Omit the object name, only one method can be called after creation
Anonymous inner class: Omit the implementation class (or subclass), only one object can be created

3. Access to anonymous internal class variables with the same name

public class External {
    
    
	//外部类成员变量
    int num = 10;
    public class Internal{
    
    
    	//内部类成员变量
        int num = 20;
        public void innerMethod(){
    
    
        	//内部类局部变量
            int num = 30;
            System.out.println(num);//就近原则,局部变量
            System.out.println(this.num);//内部类成员变量
            System.out.println(External.this.num);//外部类成员变量
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_44580492/article/details/106771470