Detailed explanation of Java internal classes (Java basics)

Foreword:

If a class is defined in a class, then the class defined in the class is called an inner class. For example, the engine is installed inside the car. If the car is defined as a car class and the engine is defined as an engine class, then the engine class is an inner class . .

1. Why use inner classes

  • The inner class provides better encapsulation, only the outer class can access the inner class
  • The inner class is not affected by the outer class and independently inherits the interface (regardless of whether the outer class implements an interface)
  • Outer classes cannot directly access inner class properties or methods, but inner classes can access outer class properties or methods (including private) methods

Inner class example:

class Text1{ //External class 
    public void meth1(){ //External member method 
        System.out.println("External class member method"); 
    } 
    class Text2{ //Inner class 
        public void nei_meth(){ 
            System.out. println("Internal class member method"); 
        } 
    } 
}

 Second, the connection between the external class and the internal class

The outer class cannot directly call the properties or methods of the inner class, as shown below:

public class Waibulei1 { //External class 
    int a = 10; //Global variable 
    public void method(){ 
        System.out.println("External class method"); 
    } 
    public static void main(String[] args) { 
       // System.out.println(b); //External classes cannot directly access internal class properties or methods, an error will be reported 
    } 
    public class Neibulei{ //Create internal class 
        int b = 10; 
        public void nei_method(){ 
            System.out.println (a); //Direct access to external class member variables 
        } 
    } 
}

 When there are private properties or methods (private) in the outer class, they can be called directly in the inner class method, for example:

class Text3{            //外部类
    private String name;
    private void p_meth(){
        System.out.println("私有方法");
    }

    public static void main(String[] args) {
        Text3 tx3 = new Text3();            //创建外部类对象
        Text3.Text4 nei_dui = tx3.new Text4();      //创建内部类对象
        nei_dui.n_method();     //调用内部类方法
    }
    class Text4{
        public void n_method(){
            System.out.println(name);       //直接访问外部私有变量
            p_meth();           //直接访问外部私有方法
        }
    }
}

As shown above, the inner class can directly call the private property or method of the outer class in the inner class method. The result of running the above code is as follows:

null
private method

 Note: The inner class object depends on the outer class object, as shown in the following two lines of code:

Text3 tx3 = new Text3(); //Create an external class object 
Text3.Text4 nei_dui = tx3.new Text4(); //Create an internal class object

Inner class objects do not appear in a class unless an outer class object already exists.

 3. How to create an inner class object

Inner class objects can be created either in the outer class or in the inner class 

The first type: external class external class object name = new external class ();

              Outer class.Inner class object name = Outer class object name.new inner class();

The second type: inner class object name = new outer class ().new inner class ();

Fourth, the classification of internal classes 

All members of inner class can be divided into: member inner class, local inner class, anonymous inner class, static inner class. 

 To learn about the classification of inner classes, go to the next article, Classification of Inner Classes.

Guess you like

Origin blog.csdn.net/qq_62414755/article/details/124431118