Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java anonymous inner classes

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Java anonymous inner class". In daily operation, I believe many people have doubts about how to use Java anonymous inner class. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Java anonymous inner classes". Next, please follow the editor to study!

Anonymous inner class:

Let me give you an example. I'll show you what an anonymous inner class is. When Endeavor first came into contact with it, I thought it was weird. It was so awkward. I don't know how everyone feels.

For comparison, let's give an example of a normal class method call (you should all understand it):

The output is as follows:

Next, let's get to the point. Anonymous inner class. Through the name, you must know what anonymous inner class is.

1. Definition: an inner class without a name (which has been introduced before).

2. What are the advantages of using internal classes? it can be summed up in one sentence: simplify writing, as for how it is simplified, where it is simplified and where it will be simplified later.

3. First of all, let's talk about when to use anonymous inner classes, even with premises and conditions: it can only be used when there is an inheritance and implementation relationship. In fact, it is also easy to understand. First of all, anonymous inner classes do not have names, so how to describe and new objects? Yes, that's right, to do this by inheriting its parent class or implementing an interface.

Here's an example:

Output result:

Seeing these lines of code, there should be no need for me to explain why anonymous inner classes can simplify writing.

It should also be curious how to call a method in an anonymous inner class. After all, it becomes a problem that an anonymous inner class does not have a class name to call a method.

1. The case where there is only one method in an anonymous inner class

2. Anonymous inner classes have a method that is easy to call, so if there are multiple methods, you don't want to do that, do you?

Try to output the results:

So how do you call multiple methods in anonymous inner classes?

There are two ways to Endeavor, if you have a better way to give more advice! 1. Using the idea of polymorphism (the premise of anonymous inner classes is that there is an inheritance or implementation relationship)

Output result:

2. Change the method type and return this

Output result:

I don't know if you have any questions after reading it. What if you want to call the unique method of the anonymous inner class? How do I call it?

Output result:

Obviously not, so Endeavor feels that the inner class of the inner name can have its own unique method, but only if there is only one method in this anonymous inner class. If there are multiple methods, he can only inherit the method of the parent class and rewrite this method or implement the interface. It is impossible to call his own unique method in the case of multiple methods, but this unique method can exist, but it cannot be called. I don't know if you agree with it or not.

For example:

Output result:

Add three points:

1. Anonymous inner class: anonymous inner class just does not have a class name, and all other members are available.

I said earlier that anonymous inner classes exist only if there is an inheritance or implementation relationship, but I don't see the extends and implements keywords. What's going on?

A: quite simply, there is no anonymous inner class, not even a class name, let alone using keywords. These were taken care of by jvm.

2. Anonymous inner class is the implementation interface

3. Next, I would like to share with you one of the most common situations in anonymous inner classes, that is, in the form of arguments.

Output result:

At this point, the study on "how to use Java anonymous inner classes" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report