The main functions of the inner class are as follows:
1. the inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class
2. the method of the inner class can access all the data of the external class directly, including the private data
3. functions implemented by internal classes can also be implemented using external classes, but sometimes it is easier to use internal classes
Inner classes can be divided into: Member inner class, Static inner class, method inner class, Anonymous inner class
How to use a member's inner class :
1, the outer class is defined inside the inner class, equivalent to the location of a member variable of the outer class, the inner class can use any access control, such as public, protected, private, etc.
2. The test () method defined in the inner class can directly access the data in the external class without being affected by the access control.
3. When creating an inner class object, you must use an external class object to create an inner class object, instead of going directly to the new inner class object, that is, the inner class object name = External class object. New inner class ();
4, after compiling the above program, you will find that two. class files have been generated
Where the second is the. class file for the outer class, the first is the. class file for the inner class, that is, the. class file for the member's inner class is always the case: the outer class name $ internal class name. class
In addition, friendly hints OH:
1. External classes cannot directly use the members and methods of an inner class, you can first create an object of the inner class, and then access its member variables and methods through the objects of the inner class.
2. If the external class and the inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variable of the external class, you can use the This keyword. Such as:
Static Inner class
Static inner classes are internal classes of static adornments, which are characterized by:
1. Static inner classes cannot directly access non-static members of an external class, but can be accessed through the new External class () member . You can also create an external object, and then pass the external object. Member access.
2. If the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. static member; If the static member of the outer class is not the same as the member name of the inner class, the static member of the outer class can be called directly through the member name
3. When creating an object of a static inner class, you do not need an object of an external class, you can directly create an inner class object name = New inner Class ();
Java Internal classes