·Java internal class summary

Inner class

1. Definition

A class defined in another class.

2. Why do you need inner classes

1. Inner classes can be hidden from other classes in the same package.

2. Inner class methods can access the data in the scope of this class, including the original private data.

3. Use internal classes to access object state

Note: Only inner classes can be private, while regular classes can have package visibility or public visibility. After the inner class is private, only the methods of the outer class of the inner class can construct the inner class object.

1. An inner class method can access its own data field, and can also access the data field of the outer class object that created it.

4. Special grammar rules for inner classes

public class demo {
    
    
    private int num;
    private String name;

    void mean1(){
    
    
        inclass var=new inclass();
    }
    void mean2(){
    
    
        inclass var=this.new inclass();
    }
    void mean3(){
    
    
        demo de = new demo()
        inclass var=de.new inclass();//显示地命名将外围类引用设置为其他对象。
    }
    public class inclass {
    
    
        public void f(){
    
    
            if(demo.this.num>0) System.out.println(1);//内部类有一个外围类的引用。
        }
    }
}

Note: All static fields declared in the inner class must be final and initialize a compile-time constant. Internal classes cannot have static methods, and the Java language specification does not provide any explanation for this limitation.

5. Local inner class

In the outer class, there may be only one method that creates an object of the inner class. When this happens, we can define this class locally in a method. Declaring a local class cannot have an access specifier (that is, public or private), and the scope of the local class is limited to the block in which the local class is declared. The local class has a big advantage, that is, it is completely hidden from the outside world, even from other codes of the inner class, only the scope block knows the existence of the inner class.

6. Anonymous inner class

When using local inner classes, you can usually go one step further. If you only want to create an object of this class, you don't even need to specify a name for the class. Such a class is called an anonymous inner class.

The syntax is as follows:

new SuperType(construction parameters)

{
    
    

​	inner class methods and data

}

SuperType can be an interface, and internal classes need to implement this interface. It can also be a class, and the inner class needs to extend this class. An anonymous inner class cannot have a constructor. In fact, the construction parameters are passed to the superclass constructor.

When generating log or debugging information, you usually want to include the class name of the current class, such as:

System.out.println(getClass());

But this does not work for static methods. Because getclass calls this.getclass. The static method does not have this.

We can use the following method

public class demo {
    
    
    private int num;
    private String name;

    public static void main(String[] args) {
    
    
        //System.err.println(this.getClass());
        System.out.println(new Object() {
    
    
        }.getClass().getEnclosingClass());
        new demo().fs();
    }
    void fs(){
    
    
        System.out.println(getClass());
    }
}

new Object(){} will create an anonymous object of the anonymous subclass of Object. getEnclosingClass() gets its enclosing class, which is the class that contains this static method.

7. Static inner class

When the inner class is declared as static, the static inner class is similar to other inner classes, except that the object of the static inner class does not generate a reference to its outer class object. Unlike regular classes, static inner classes can have static fields and methods. The inner classes declared in the interface are automatically static and public.

Guess you like

Origin blog.csdn.net/changbaishannefu/article/details/113113776