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 understand anonymous inner classes in Java inner classes

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to understand the anonymous inner class in the Java inner class? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Anonymous inner class

Anonymous inner classes should be the ones we use most when writing code. Using anonymous inner classes when writing code for event monitoring is not only convenient, but also makes the code easier to maintain. The following code is a piece of Android event listening code:

Scan_bt.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {/ / TODO Auto-generated method stub}}) History_bt.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {/ / TODO Auto-generated method stub}})

This code sets listeners for two buttons, which uses anonymous inner classes. In this code:

New OnClickListener () {@ Override public void onClick (View v) {/ / TODO Auto-generated method stub}}

Is the use of anonymous inner classes. In the code, you need to set a listener object for the button, and using anonymous inner classes can generate a corresponding object while implementing the methods in the parent class or interface, but only if the parent class or interface exists before it can be used in this way. Of course, it is possible to write something like this, which is the same as using anonymous inner classes above.

Private void setListener () {scan_bt.setOnClickListener (new Listener1 ()); history_bt.setOnClickListener (new Listener2 ());} class Listener1 implements View.OnClickListener {@ Override public void onClick (View v) {/ / TODO Auto-generated method stub}} class Listener2 implements View.OnClickListener {@ Override public void onClick (View v) {/ / TODO Auto-generated method stub}}

Although this writing method can achieve the same effect, it is lengthy and difficult to maintain, so it generally uses the method of anonymous inner class to write event listening code. Similarly, anonymous inner classes cannot have access modifiers and static modifiers.

Anonymous inner classes are the only classes that do not have a constructor. Because it has no constructor, the scope of use of anonymous inner classes is very limited, and most anonymous inner classes are used for interface callbacks. The anonymous inner class is automatically named Outter$1.class by the system at compile time. In general, anonymous inner classes are used to inherit other classes or implement interfaces without adding additional methods, just the implementation or rewriting of inherited methods.

This is the answer to the question on how to understand the anonymous inner class in the Java inner class. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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

Internet Technology

Wechat

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

12
Report