Inner class
Most of the time, we define the class as a separate program unit. In some cases, we need to place a class within the definition of another class, which is defined inside other classes as inner classes (nested classes), and classes containing inner classes are called external classes (host classes). The inner class has the following functions
- The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow access to other classes of the same package. Suppose you create the Cow class, you need to combine the properties ofthe cowleg ,cowleg property is valid only in the Cow class, leaving the Cow class is meaningless, in which case the Cowleg can be defined as an inner class of Cow, and no other external classes are allowed to access
- Inner class members have direct access to private data inside the outside because the inner class is treated as a member of its outer class and can be accessed between members of the same class. However, the implementation details of the inner class, such as properties, cannot be accessed externally. To access an object that must create an inner class.
- Anonymous inner classes are appropriate to create classes that only need to be used once
And look back on the tenth day of Java