Java SE --- inner classes

inner class

1. Overview of inner classes

An inner class refers to a class defined inside another class, and the class is called an inner class. The class name does not need to be the same as the source file name .

Types of inner classes:
In Java, there are two situations in which inner classes are used:

(1). Define a class in a class (member inner class, static inner class);
(2). Define a class in a method (local inner class, anonymous inner class).

2. Local inner class

Local inner class (local inner class): A class defined in a method.
It can only be used in the current method, and cannot be used outside the method.

public class TestDemo {
    
    
    public void func() {
    
    
        class In{
    
    
            private int a;
            public String str;
        }
        //局部内部类 缺点 :只能在当前方法中使用
    }
}

3. Member inner class

A member inner class is also called an instance inner class, which can be regarded as a member of an ordinary instance of the outer class.

class OuterClass {
    
    
    public int data1 = 1;
    private int data2 = 2;
    public static int data3 = 3;

    // 实例内部类
    class InnerClass{
    
    
        public int data4 = 4;
        private int data5 = 5;
        public static final int data6 = 6;

        public InnerClass() {
    
    
            System.out.println("不带参数的内部类的构造方法");
        }

        public void test() {
    
    
            System.out.println("InnerClass::test()");
        }
    }

    public void func(){
    
    
        System.out.println("OuterClass::func1()");
    }
}

Precautions:

1. Cannot

insert image description here
If you have to define it, you can only define one, static constant. (static final)
insert image description here

2. How to instantiate an object of a member inner class

外部类名.内部类名 变量 = 外部类对象的引用.new 内部类();
insert image description here

3. In a member inner class, if a member variable with the same name as the outer class is included, how to access it in the member inner class

insert image description here

4. How to inherit inner classes (just understand)

insert image description here

5. When a class has an inner class, the bytecode file (just understand)

OuterClass$InnerClass

外部类$内部类.class
insert image description here

4. Static inner class

class OuterClass {
    
    
    public int data1 = 1;
    private int data2 = 2;
    public static int data3 = 3;
    // 静态内部类
    static class Innerclass{
    
    
        public int data4 = 5;
        private int data5 = 5;
        private static int data6 = 6;

        public void test(){
    
    
            System.out.println("InnerClass::test()");
        }
    }
}

Precautions:

1. How to instantiate an object of a static inner class

外部类名.内部类名 变量 = new 外部类名.内部类名();
insert image description here

2. In a static inner class, how to access the member variables of the outer class

insert image description here

5. Anonymous inner classes

class Out{
    
    
    public void test(){
    
    
        System.out.println("hello!");
    }
}


public class Test {
    
    
    public static void main(String[] args) {
    
    
        new Out(){
    
    

        };
    }
}

insert image description here
The anonymous inner class calls the test() method.
insert image description here
The anonymous inner class calls the overridden test() method
insert image description here

Guess you like

Origin blog.csdn.net/wwzzzzzzzzzzzzz/article/details/123145771