[introduction to Java] Day8 Java inner class — anonymous inner class

Today, let's look at another more magical class - anonymous inner class.

As its name indicates, this class is anonymous. After it is used up, it hides its skills and name silently in the mountains like a sweeping monk. The anonymous inner class not only has no name, but also omits the class keyword. Moreover, the anonymous inner class must inherit from a class or implement an interface. It looks like this:

Look at a chestnut:

This is an abstract class. If anonymous inner classes are used to inherit, it is as follows:

Simple and crude, it looks like a simplified version of a local inner class. What happens if you don't use anonymous inner classes?

We need to create a class to inherit this abstract class:

Then use this class:

Because a separate class is often placed in a separate file, if this class only needs to create an object, it will be overqualified. From the chestnuts above, we can compare an advantage of anonymous internal class: it is more simple and convenient when the class only needs to create an object.

Another practical Chestnut:

Here, an anonymous inner class inherited from thread is created, which overwrites the run method, creates an instance, returns it to t, and then calls the run method. You can see that there can only be one instance object in the anonymous inner class, because new cannot be created once, and you may feel that the local inner class is very limited, Why are anonymous inner classes smaller than local inner classes

You don't understand. In the actual use of Java, anonymous inner classes are very useful. Why use anonymous inner classes?

Sometimes, the class we create only needs one instance. For example, in multithreading, to use multithreading, we usually inherit the thread class or implement the runnable interface, and then call its methods. Each task is generally different. It is obviously difficult to manage to create a new class every time, because each class is lost only once, At this time, it is very convenient to use anonymous inner classes. It not only does not need to manage a pile of one-time classes, but also it is simple and rough to create. Like the above chestnuts, they can be simplified as follows:

Call the run method directly after creating the instance, which is simple and crude.

Anonymous inner classes can not only inherit from classes, but also implement interfaces. For example:

Of course, there are cases where you have to use internal classes. Classes can only inherit from one class. If a class needs to use a protected method of another class in another package, but it has inherited from another class, you have to use internal classes at this time.

For example, there is also a woman category:

At this time, if man can't help being lonely and wants to dance, what should he do? Inheriting the woman class is obviously illogical and can't be realized, because he has inherited from the human class, but what should he do if he wants to dance?

The appearance of inner classes makes this problem very simple:

Because under different packages, the dance method of woman cannot be used directly, but it can be inherited by internal classes to call the protected method and then put it into the man method. In this way, man can dance like woman:

Of course, there are many restrictions on using anonymous inner classes:

1. Anonymous inner classes must inherit a class or implement an interface, but they cannot have both. At the same time, they can only inherit a class or implement an interface.

2. Anonymous inner classes cannot define constructors.

3. No static member variables or static methods can exist in an anonymous inner class.

4. Anonymous inner classes are special local inner classes, so all restrictions on local inner classes also apply to anonymous inner classes.

5. Anonymous inner classes cannot be abstract. They must implement all abstract methods of inherited classes or implemented interfaces.

So the question is, how to initialize an anonymous inner class? After all, anonymous inner classes cannot have constructors.

Of course, first of all, you can use the initialization block to implement it, like this:

However, this is obviously more rigid, not flexible enough, and can not accept external parameters. So how to use it flexibly? Don't worry. There are always more methods than problems. There are still ways to solve them:

Here, the initialization block is used to initialize the anonymous internal class. Note that if the anonymous internal class needs to use external parameters or variables, the final modification must be used, because the internal class actually uses a copy of the parameters, not the parameters themselves. In order to more clearly indicate that the parameters are immutable, The compiler will require the final keyword to decorate the variables that need to be used.

So far, the anonymous 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
分享
二维码
< <上一篇
下一篇>>