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

Instance usage of Java Anonymous object and Anonymous Inner Class

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

Share

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

This article introduces the knowledge of "instance usage of Java anonymous objects and anonymous inner classes". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Anonymous object: an object without a name.

Non-anonymous objects:

ClassName c=new ClassName ()

C.run ()

Anonymous object:

New ClassName () .run ()

Note:

1. When an object makes only one call to a method, it can be reduced to an anonymous object.

2. Two anonymous objects cannot be the same object.

3. Property values are generally not assigned to anonymous objects because they can never be obtained.

4. Once run, it will be recycled directly, saving memory space.

An example of code used by anonymous objects:

Public class Anony {int astat1; int bread2; void run () {System.out.println (aqb);} public static void main (String [] args) {new Anony () .aaccount10; / / Anonymous objects cannot be reassigned, and the assignment still fails Anony a=new Anony (); a.run () / / call the method new Anony () .run () by creating an object; / / create an object anonymously and call the method}}

Running result:

three

three

Anonymous inner class: an anonymous inner class is an inner class without a name.

Format:

ClassName object=new ClassName () {

/ * Code block * /

}

Note:

1. An anonymous inner class must inherit a parent class or implement an interface.

Abstract class code example: (interface is the same)

Abstract class AnonyTest {int astat1; int bread2; public abstract void run ();} public class AnonyInner {public static void main (String [] args) {AnonyTest a=new AnonyTest () {/ / Abstract anonymous class public void run () {System.out.println (aquib) }; a.run ();}}

If you do not use anonymous inner classes to implement abstract methods:

Abstract class AnonyTest {int astat1; int bread2; public abstract void run ();} class AnonyDemo extends AnonyTest {public void run () {System.out.println (aqb);}} public class AnonyInner {public static void main (String [] args) {AnonyTest a=new AnonyDemo (); / / upload object a.run ();}}

Running result:

three

This is the end of "instance usage of Java Anonymous objects and Anonymous Inner classes". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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