You can place the definition of one class inside the definition of another class, which is the internal class.
Internal classes are a very useful but difficult feature to understand (I haven't used them very much so far, and I know only a little about them).
First meeting
Internal classes are very easy to understand from the outside, just to define a class inside a class.
InnerClass is the inner class here. For beginners, the inner class is really not used much. The same is not used by the beginner (it seems only used in swing registration events), but as the programming ability improves, we will understand its charm. It can use more elegant design of our program structure. . Between using internal classes, we need to understand why we use internal classes and what benefits internal classes can bring us.
1. Why use internal classes
Why use internal classes? In Think in Java > There is a saying that the most attractive reason for using inner classes is that each inner class can inherit an implementation independently, so whether or not the outer class has inherited an implementation has no effect on the inner class.
In our programming, there are some problems that are difficult to solve by using interfaces. At this time, we can solve these programming problems by utilizing the ability of internal classes to inherit multiple concrete or abstract classes. It can be said that interfaces solve only part of the problem, while internal classes make the solution of multiple inheritance more complete.
In fact, we can't see the advantages of using internal classes for this example, but what if Father and Mother are not interfaces, but abstract classes or concrete classes? At this point, we can only use internal classes to achieve multiple inheritance.
In fact, the greatest advantage of using internal classes is that it can solve the problem of multiple inheritance very well, but if we do not need to solve the problem of multiple inheritance, then we can naturally use other coding methods, but using internal classes can also bring us the following features (from Think in java):
Internal classes can use multiple instances, each of which has its own state information and is independent of the information of other peripheral objects.
2. In a single peripheral class, multiple internal classes can implement the same interface in different ways, or inherit the same class.
3. The creation of internal class objects does not depend on the creation of peripheral class objects.
4. There is no confusing "is-a" relationship between the inner class and it is an independent entity.
5. Internal classes provide better encapsulation. Except for the peripheral classes, no other classes are accessible.
Internal Class Foundation
This section focuses on how internal classes use the attributes and methods of external classes, as well as the use of. this and. new.
When we create an inner class, it has an invisible connection with the outer class, depending on which it can access the elements of the outer class without restriction.
In this application, we can see that InnerClass can seamlessly access the attributes of the peripheral class OuterClass, although it is private ly modified. This is because when we create an internal class object of a peripheral class, the internal class object must capture a reference to that peripheral class object, which is used to select the members of the peripheral class whenever we access the members of the peripheral class.
In fact, in this application we also see how to refer to internal classes: referring to internal classes we need to specify the type of the object: OuterClasName.InnerClassName. At the same time, if we need to create an internal class object, we must use the object of the external class to create an internal class through. new: OuterClass.InnerClass innerClass = outerClass.new InnerClass();.
At the same time, if we need to generate a reference to an external class object, we can use OuterClassName.this, so that we can produce a reference to the external class correctly. Of course, this is known at compile time, and there is no runtime cost.
At this point, we need to make it clear that internal classes are a compile-time concept, and once compiled successfully, they belong to two completely different classes (of course, they are related to each other). For a peripheral class named OuterClass and an internal class named InnerClass, after successful compilation, two class files will appear: OuterClass.class and OuterClass$InnerClass.class.
Internal classes in Java are mainly divided into member internal classes, local internal classes, anonymous internal classes and static internal classes.
Internal Classes of Members
The member inner class is also the most common inner class. It is a member of the peripheral class, so it can access all the member attributes and methods of the peripheral class without restriction. Although it is private, the member attributes and methods of the peripheral class need to be accessed through the inner class instance to access the member attributes and methods of the inner class.
There are two points to be noted in the inner class of a member: first, there can be no static variables and methods in the inner class of a member; second, the inner class of a member is dependent on the outer class, so the inner class can only be created by creating the outer class first.
getxxx() is recommended to obtain member inner classes, especially when the constructor of the inner class has no parameters.
Local internal classes
There is such an internal class, which is nested in methods and functions. The use of this class is mainly to apply and solve more complex problems. We want to create a class to assist our solution. At that time, we don't want this class to be publicly available, so we have local internal classes, which are compiled just like member internal classes. Its scope has changed, it can only be used in this method and attributes, out of which the method and attributes will be invalid.
There are really no good examples for local internal classes, so we quote the classic example in Think in java.
Defined in the method:
Define in scope:
5. Anonymous Internal Classes
In Swing programming, we often use this way to bind events
At first glance, we may find it very strange, because this inner class has no name. Look at the following example:
Here we need to see a few places.
1. Anonymous inner classes have no access modifiers.
2. new anonymous inner class, which is the first class to exist. If we annotate that InnerClass interface, there will be a compilation error.
3. Note the parameters of the getInnerClass() method. The first parameter is modified with final, but the second parameter is not. At the same time, we also find that the second parameter has not been used in the anonymous inner class, so when the parameter of the method needs to be used by the anonymous inner class, the parameter must be final.
4. Anonymous inner classes are not constructed. Because it has no way to construct names.
PS: Because of the limited space, this is the introduction of anonymous inner classes. For more information about anonymous inner classes, I will give a detailed introduction in the next blog (java Enhancement - - - Explaining anonymous inner classes in detail), including why the formal parameters should be defined as final, how to initialize anonymous inner classes, etc. Please look forward to...
Sixth, Static Internal Classes
Static can modify member variables, methods, code blocks, and other internal classes. Static can modify internal classes, which we call static internal classes, but we prefer to call nested internal classes. There is a big difference between static inner classes and non-static inner classes. We know that non-static inner classes implicitly store a reference after compilation. The reference refers to the periphery of creating it, but static inner classes do not. Without this reference, it means:
1. Its creation does not depend on peripheral classes.
2. It cannot use non-static member variables and methods of any peripheral class.
The above example fully demonstrates the difference between static internal classes and non-static internal classes.
The introduction of internal classes is almost over here! In fact, I know only the fur for the inner class, approaching a rookie, and my knowledge is limited! I will use these days to study the internal classes!