Hello everyone, I am xiao CAI, a desire to do CAI Not CAI xiao CAI in the Internet industry. Soft but just, soft praise, white piao just! “Dead ghost ~ see finish remember to come to me 3 even!”
“
This article focuses on the use of inner classes in Java
Refer to it if necessary
If it is helpful, do not forget to “praise” Sunday
Wechat public number has been opened, “xiao CAI Liang Ji”, do not pay attention to small partners remember to pay attention to oh!
“
Today again Friday ah, is thinking tomorrow Saturday what arrangement, a surprise interrupted me
Xiao CAI xiao CAI, look at this set of code, so flexible
Hear lead desk small wang’s surprise, I turned to see his screen, this is not the interior kind yao. With good of course flexible, but we usually did not how to use.
Internal class with good really good flexible ah, I am not very familiar with this piece, it seems to have to learn more! Xiao CAI, you seem to know a lot about it. Can you tell me something about it?
Looking at Xiao Wang’s hungry eyes, I couldn’t help feeling a little guilty, and my inner activities were also extremely complicated: I didn’t use them at ordinary times, but I just had a general understanding, and it would be embarrassing to speak out, so I hurriedly said:
It's easy to say, but today is Friday, so it's not too bad. Let's think about what we have in store for tomorrow, and I'll tell you all about it next week.
Xiao Wang seemed to be my fool in the past, did not see my eyes in panic, agreed to come down.
Fortunately, what can be arranged over the weekend, hurriedly arrange the internal class!
I met
Interfaces and inner classes are more complex than other concepts in object-oriented programming, such as C++. Put the two together and you can solve a problem that C++ can solve with multiple inheritance. However, multiple inheritance has proved to be quite difficult to use in C++. Java’s interfaces and inner classes are much easier to understand by comparison!
How to create an inner class
An inner class, as the name implies, is a class within a class, which defines the class inside the outer class:
becauseMonkey
andPig
The two classes are defined inAnimal
Class, so using these two inner classes is no different from using normal classes. The following code is familiar to you:
Return a reference to the executing inner class by defining a method. If you’ve noticed, the inner class reference is a little weird: animal.monkey. This is also one of the inner class difference, if you want to outer class non-static methods to obtain an inner class object, the need to specify the object type: OuterClassName. InnerClassName
Two, connected inside and outside
Inner classes exist in the inner layer of the outer class, so they have certain privileges: the inner class can access all the members of the outer object without any special conditions, and the inner class has access to all the elements of the outer class.
As you can see from the previous set of code, InnerArray can access each of the properties in the OuterArray as if it owned them, which is a great convenience.
3. New and this
We are certainly not unfamiliar with these two keywords, we usually use the most affirmation is a new object.
public class OuterClass {
class InnerClass {
}
public static void main(String[] args) { OuterClass outer = new OuterClass(); } } Copy the code
When we need an OuterClass object, we get a new OuterClass(), but what if we need an InnerClass object? The answer is:
InnerClass inner = outer.new InnerClass();
Copy the code
It might seem strange that new here needs to be referenced as an OuterClass object, because the inner class object is implicitly connected to the OuterClass object that created it, so you have to use the OuterClass object to create the inner class object. If you create a “nested class” (static inner class), it does not need a reference to an external class object.
The this keyword is used to generate a reference to an external class object that automatically has the correct type:
Local inner class
The inner classes we saw above are defined in the outer class, which is the typical use of inner classes. However, we can also define inner classes in a method or in any scope. This is also called a local inner class:
The Pig class is part of the getPig() method, not the OuterClass, so it can’t be accessed outside of getPig().
Anonymous inner classes
Before we look at what an anonymous inner class is, let’s look at a set of code:
Animal () this method combines the generation of the return value with the class definition that represents the return value. And this class is anonymous, it has no name, the normal form should look like this:
Anonymous class revisiting factory:Get an implementation of an outer class from an inner class like thisImplementation1
Can beprivate
, and there is no need to create concrete classes as factories, so the resulting syntax is more practical and can also be used in singleton patterns.
6. Nested classes
An inner class can be declared static if there is no need for an inner class object to be related to a enclosing class, which is often called a nested class. A normal inner class object implicitly stores a reference to the enclosing class object that created it. However, when the inner class is static, that means:
- To create an object of a nested class, you do not need an object of its enclosing class
- A non-static enclosing class object cannot be accessed from an object of a nested class 在
main()
There’s nothing in the methodNestClass
Objects are required, instead using a selectionstatic
Member’s normal syntax to call a method.
Interface inner class
Normally, you can’t put any code inside an interface, but nested classes can be part of the interface. Any class you put in an interface is automatically public and static. Because classes are static, you just put nested classes in the namespace of the interface, which does not violate the rules of the interface. You can even implement an interface from an inner class to its outer class:
If you want to create some common code that can be shared by all different implementations of an interface, it can be convenient to use nested classes inside the interface, although they are available after Java 8default
To implement interface methods by default.
Inherit inner classes
Inner classes as a class are certainly allowed to be inherited. But because the constructor of an inner class must be connected to a reference to its enclosing class object, the reference to the enclosing class object must be initialized when the inner class is inherited, and there is no longer a concatenable default object in the exported class:
As you can see, an error can be reported by inheritance. The solution is:
class ExtendClass {
class Inner{}
}
class WithInner extends ExtendClass.Inner {
public WithInner(ExtendClass extendClass) { extendClass.super(a); } } Copy the code
So we need to remember that if we want to inherit an inner class, we must use the outer class.super() inside the constructor so that the necessary references are provided and the program can compile.
Override inner class?
When a subclass inherits from a parent class, a subclass can override the methods of the parent class. So the question is, can inner classes be overridden? Let’s find out by looking at a set of code:
As you can see from this example, nothing magical happens to the inner class when a enclosing class is inherited, and the two inner classes are completely separate entities, each in its own namespace.
Why use inner classes?
Before we answer that question, let’s know one thing:
“Each inner class can inherit an implementation independently, so it doesn’t matter whether the enclosing class already inherits an implementation“
This statement clearly illustrates the power of inner classes. Some design and programming problems would be difficult to solve without the ability of inner classes to inherit from multiple concrete or abstract classes. In this sense, inner classes complete multiple inheritance solutions. Interfaces solve part of the problem, effectively implementing “multiple inheritance” for inner classes.
Shout ~ finally the internal class review of almost, at first glance, today is the weekend! It seems that the weekend did not arrange the plan, but this week is quite substantial, the foundation consolidated once, Monday when you can talk with Xiao Wang, think of here, xiao CAI can not help but also into infinite fantasy!
“
Today you work harder, tomorrow you will be able to say less words!
I am xiao CAI, a man who studies with you. 💋
Wechat public number has been opened, “xiao CAI Liang Ji”, did not pay attention to the students remember to pay attention to oh!
“