Java internal class usage study notes

   In Java, it is allowed to define a class in the inner class of a class. Such a class is called an inner class, and the class in which the inner class is located is called an outer class. According to the position of the inner class, the modifier and the way of definition can be divided into member inner class, static inner class and method inner class.

  • Member inner class
       We know that in a class, you can define construction methods, member methods, member variables, and also define classes. This class is called an inner class. The inner class can access all the members of the outer class.
    Examples:
public class Example08 {
    
    
    public static void main(String[] args) {
    
    
        //创建外部类对象
        OuterClass outerClass = new OuterClass();
        //调用外部类方法
        outerClass.outerTest();
    }
}
class OuterClass{
    
    
    //定义一个成员变量
    int num = 10;
    //定义一个方法,方法中访问内部类
    public void outerTest(){
    
    
        InnerClass innerClass = new InnerClass();
        innerClass.innerTest();
    }
    //定义一个内部类
    class InnerClass{
    
    
        public void innerTest(){
    
    
            System.out.println("我是成员内部类我可以访问外部类成员--" + num);
        }
    }
}

Operation result:
Insert picture description here
If we want to access the internal class with the external class, we need to create the internal class object through the external class object, the syntax format:

External class name. Internal class name Variable name = new external class().new internal class();

We modify the above example to the following code:

public class Example08 {
    
    
    public static void main(String[] args) {
    
    
        /*//创建外部类对象
        OuterClass outerClass = new OuterClass();
        //调用外部类方法
        outerClass.outerTest();*/
        OuterClass.InnerClass innerClass = new OuterClass().new InnerClass();
        innerClass.innerTest();
    }
}

The running result remains unchanged:
Insert picture description here

  • A static inner class
        uses the static keyword to modify a member inner class, which is called a static inner class. A static inner class can be instantiated without creating an outer class. The syntax format is as follows:

External class name. Internal class name Variable name = new external class name. Internal class name();

Examples:

public class Example08 {
    
    
    public static void main(String[] args) {
    
    
        //创建内部类对象
        OuterClass.innerClass innerClass = new OuterClass.innerClass();
        //调用内部类对象
        innerClass.print();
    }
}
class OuterClass{
    
    
    //定义一个静态内部类
    static class innerClass{
    
    
        //定义一个成员变量
        private static int num = 10;
        public void print(){
    
    
            System.out.println("num = " + num);
        }
    }
}

There are two points to note when using static inner classes:

  1. The static inner class can only access the static members of the outer class.
  2. Only static members can be defined in static inner classes, while static members cannot be defined in non-static inner classes.
  • Method inner class The class
        defined in the method is called the method inner class, and this class can only be used in the method.
    Examples:
public class Example08 {
    
    
    public static void main(String[] args) {
    
    
        OuterClass outerClass = new OuterClass();
        outerClass.print();
    }
}
class OuterClass{
    
    
    //定义一个成员变量
    private int num = 20;
    public void print(){
    
    
        //定义一个方法内部类
        class MethodInnerClass{
    
    
            //定义一个方法
            public void print(){
    
    
                System.out.println("num = " + num);
            }
        }
        //创建内部类对象并调用内部类方法
        new MethodInnerClass().print();
    }
}

Come on! ! !

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/109210698