Java: Detailed explanation and summary of the four internal classes

statement

1) Part of the content of this article is compiled from the information on the Internet. If you accidentally infringe on everyone's rights, you can still contact the blogger to delete it.

2) The blogger is Mengxin on the road. If there is any inappropriateness in the article, please point it out and make progress together, thank you.


In Java, a class can be defined in another class or in a method. Such a class is called an inner class. Inner classes in a broad sense generally include these four types: member inner classes, local inner classes, anonymous inner classes, and static inner classes. Let's first understand the usage of these four internal classes.

Member inner class

The member inner class is the most common inner class. It is defined as being located inside another class and has the following form:

class Animal {
    
    
    String name;

    public Animal(String name) {
    
    
        this.name = name;
    }

    class Rabbit {
    
                                  //内部类
        public void myRabbit() {
    
    
            System.out.println("Rabbit.");
        }
    }
}

In this way, the Rabbit class is like a member of the Animal class, and Animal is called an external class. The member inner class can unconditionally access all member attributes and member methods of the outer class (including private members and static members).

class Animal {
    
    
    private String name = "rabbit";
    public static int age =1;
    public Animal() {
    
    
    
    }

    class Rabbit {
    
                                                     //内部类
        public void Message() {
    
    
            System.out.println("The animal's name is "name);       //外部类的private成员
            System.out.println("He is "age);                       //外部类的静态成员
        }
    }
}

However, it should be noted that when a member variable or method with the same name is hidden, the member of the inner class of the member is accessed by default. If you want to access a member of the same name of an external class, you need to access it in the following form:

外部类.this.成员变量
外部类.this.成员方法

Although the member inner class can unconditionally access the members of the outer class, and the outer class wants to access the members of the member inner class, it is not so arbitrary. If you want to access the members of the inner class of the member in the outer class, you must first create an object of the inner class of the member, and then access it through a reference to this object:

public class Animal {
    
    
    private String name = "rabbit";
    public static int age =1;
    public Animal() {
    
    
        getRabbitInstance().Message();  //必须先创建成员内部类的对象,再进行访问
    }

    private Rabbit getRabbitInstance(){
    
    
        return new Rabbit();
    }

    class Rabbit {
    
         //内部类
        public void Message() {
    
    
            System.out.println(name);  //外部类的private成员
            System.out.println(age);   //外部类的静态成员
        }
    }
}

The member inner class is dependent on the outer class, that is, if you want to create an object of the member inner class, the premise is that there must be an object of the outer class. The general way to create a member inner class object is as follows:

public class Test {
    
    
    public static void main(String[] args)  {
    
    
        Outter outter = new Outter();
        //第一种方式:
        Outter.Inner inner = outter.new Inner();  //必须通过Outter对象来创建
         
        //第二种方式:
        Outter.Inner inner1 = outter.getInnerInstance();
    }
}
 
class Outter {
    
    
    private Inner inner = null;
    public Outter() {
    
    
         
    }
     
    public Inner getInnerInstance() {
    
    
        if(inner == null)
            inner = new Inner();
        return inner;
    }
      
    class Inner {
    
    
        public Inner() {
    
      
        
        }
    }
}

Inner classes can have private access, protected access, public access, and package access.

For example, in the above example, if the member inner class Inner is decorated with private, it can only be accessed inside the outer class; if it is decorated with public, it can be accessed anywhere; if it is decorated with protected, it can only be accessed in the same package or Access under the condition of inheriting the external class; if it is the default access permission, it can only be accessed under the same package.

This point is different from external classes, which can only be modified by public and package access permissions. I personally understand it this way. Since the inner class of the member looks like a member of the outer class, it can have multiple permissions modification like a member of the class.

Local inner class

A local inner class is a class defined in a method or a scope. The difference between it and a member inner class is that the access of the local inner class is limited to the method or the scope.

class People{
    
    
    public People() {
    
    
         
    }
}
 
class Man{
    
    
    public Man(){
    
    
         
    }
     
    public People getWoman(){
    
    
        class Woman extends People{
    
           //局部内部类
            int age =0;
        }
        return new Woman();
    }
}

Note that a local inner class is like a local variable in a method, and it cannot have public, protected, private, and static modifiers.

Anonymous inner class

Anonymous inner classes should be the most commonly used when we write code. It is not only convenient to use anonymous inner classes when writing event monitoring code, but also makes the code easier to maintain.

An anonymous inner class is the only class without a constructor. Because it has no constructor, the scope of use of anonymous inner classes is very limited, and most anonymous inner classes are used for interface callbacks. The anonymous inner class is automatically named Outer$1.class by the system when it is compiled. Generally speaking, anonymous inner classes are used to inherit other classes or implement interfaces, and do not need to add additional methods, but only implement or rewrite inherited methods.

Static inner class

A static inner class is also a class defined in another class, except that a keyword static is added in front of the class. The static inner class does not need to depend on the outer class. This is similar to the static member properties of the class, and it cannot use the non-static member variables or methods of the outer class. This is easy to understand, because there is no object of the outer class In the case of a static inner class, you can create an object of a static inner class. If you allow access to the non-static members of the outer class, there will be a contradiction, because the non-static members of the outer class must be attached to the specific object.

public class Test {
    
    
    public static void main(String[] args)  {
    
    
        Outter.Inner inner = new Outter.Inner();
    }
}
 
class Outter {
    
    
    public Outter() {
    
    
         
    }
     
    static class Inner {
    
    
        public Inner() {
    
    
             
        }
    }
}

Summary of internal classes

Ordinary inner class

Non-static class, defined in a member variable position of a class

After definition, it can be used in the class

Inheritable classes implement interfaces

-Normal internal classes cannot define static variables (unless it is a constant public final static int a = 5;)

-Separate ordinary inner class objects that are not associated are not allowed (you must first create an instance of the outer enclosing class. New method to create)

-Use private/package private/protected/public to control outside access

After compilation, the corresponding class will be generated in the file: class name + $ + internal class name

Local inner class

Non-static classes defined in code blocks, such as methods, for loops, if statements, etc.

Famous after definition → can create objects for use, and add constructor

-Local internal classes cannot define static variables (unless it is a constant public final static int a = 5;)

-Access to members of the enclosing class

But can only be used in this code block

After compilation, the corresponding class will be generated in the file: class name + $ + serial number + internal class name

Cannot define an interface

Anonymous inner class

-Can inherit, rewrite, and supplement the constructor of the parent class/parent interface (with parameters)

-Anonymous inner class cannot define static variables (unless it is a constant public final static int a = 5;)

-Anonymous inner classes cannot define static methods

-The class name should be added when accessing the peripheral variables of the class (you can access the member variables and methods of the surrounding class)

-If the anonymous inner class is defined in a static method, then they can only access the static members of the enclosing class

-Anonymous → The outer enveloping class and other classes cannot access the anonymous inner class

After compilation, the corresponding class will be generated in the file: class name + $ + number number

Static nested class

-The level is the same as the member variables/methods of the enclosing class

-The third party can access the static nested class only through the outer enclosing class:Outer1.Inner1 obj = new Outer1.Inner1();

-Static nested classes can directly access the static members of the enclosing class

-Static nested classes can access non-static members of enclosing classes through objects

-The use of private/package private/protected/public to control external access is essentially the same as the top-level class

Guess you like

Origin blog.csdn.net/weixin_46263782/article/details/107685347