Object-oriented ------------- anonymous internal class

Source: Internet
Author: User

1. Definitions of anonymous internal classes: in a class, another class is called an internal class. Anonymous internal class: If a class is used only once in the entire operation, it can be defined as an anonymous internal class. Anonymous internal classes are developed based on abstract classes and interfaces.

Interface A {public void printInfo (); // defines the interface} class B implements A {// implements the public void printInfo () {System. out. println ("Hello World !!! ") ;}}; Class X {public void fun1 () {this. fun2 (new B ();} public void fun2 (A a) {. printInfo () ;}}; public class NoInnerClassDemo01 {public static void main (String args []) {new X (). fun1 (); // instantiate the object of Class X and call the fun1 () method }};

 

If Class B is only used once, it is unnecessary to define a class for it. In this case, you can use an anonymous internal class.
Interface A {public void printInfo (); //} class X {public void fun1 () {this. fun2 (new A () {public void printInfo () {System. out. println ("Hello World !!! ") ;}}) ;}Public void fun2 (A a) {. printInfo () ;}}; public class NoInnerClassDemo02 {public static void main (String args []) {new X (). fun1 (); // instantiate the object of Class X and call the fun1 () method }};

 

2. Conclusion 1. Various development frameworks are often used in Java Development, and a large number of anonymous internal classes are referenced in the frameworks. 2. Anonymous internal classes are developed based on abstract classes and interfaces.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.