[introduction to Java] Day6 Java inner class – member inner class

What the inner class is, in short, is the class defined inside the class (serious nonsense).

A serious inner class looks like this:

This is a class written for demonstration. There is no Luan use. You can see that the inner class is placed inside the outer class.

So why bother defining a class inside a class? Can't you define a class outside of a class? What is the relationship between inner classes and outer classes?

Indeed, many times, it is more convenient and common to define a class externally, but the existence of an internal class naturally has its own reason. As a class parasitic on an external class, an internal class can freely access all the properties and methods of the external class. Is there any association with the inheritance we mentioned earlier? A subclass can inherit the properties and methods of the parent class, However, internal classes have higher access rights. Not only the public attribute, the protected attribute, but also the private attribute can be accessed easily, so that the modifier will not limit your imagination (manual funny).

Internal classes are also divided into many types: member internal classes, static internal classes, local internal classes, and anonymous internal classes. Today, let's talk about the first one: member inner class.

The member inner class is the simplest and crudest inner class. The inner class in the chestnut above is the member inner class. Compared with ordinary classes, the member inner class cannot have members or methods decorated with static. Because the member inner class is parasitic in the outer class, there must be an external class instance before there can be a member inner class. When creating an internal class object externally, the posture is also different. It looks like this:

Internal classes can access methods and properties of external classes, and external classes can also access methods and properties of internal classes. For example:

The output is as follows:

The posture of an external class accessing an internal class is to generate an internal class instance first, and then access all methods and properties. When an internal class accesses an external class method and property, it directly uses outer Property / method name is enough.

In this chestnut, we can see that the inner can have unrestricted access to the properties of the outer class, although it is private decorated. Because when we create an internal class object of a peripheral class, we will pass in a reference of the external class to the internal class. As long as we access the members of the external class, we will use this reference to select the members of the peripheral class. When referencing an internal class, you need to use outer Instead of using inner directly (except in the outer class), it is recommended to use getinnerinstance to obtain the inner class, especially when the inner class has only a parameterless constructor:

In this way, it can be better encapsulated.

Of course, there is another important difference between internal classes and external classes: internal classes can be modified with private, while external classes cannot be modified with private. If the internal class is only used inside the class, the private modification can better hide the internal information.

So far, the first part of the internal class has been explained. Welcome to continue your attention.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>