[Object-oriented (advanced)] _ anonymous internal class notes

Source: Internet
Author: User

Measure the test taker's knowledge about the functions of anonymous internal classes.
Measure the test taker's knowledge about the definition formats of anonymous internal classes.

3. Details:
Internal class: there is another class inside a class called internal class, so the anonymous internal class, if a class is used only once in the entire operation, it can be defined as an anonymous internal class. The anonymous internal class is developed based on the abstract class and interface.

 
Interface A {public void printinfo ();} Class B implements a {// implementation interface public void printinfo () {system. Out. println ("Hello world !!! ") ;}} Class X {public void fun1 () {This. fun2 (New B ();} public void fun2 (A) {. printinfo () ;}} public class noinnerclassdemo01 {public static void main (string ARGs []) {new x (). fun1 (); // instantiate the X object and call the fun1 () method }}

If Class B is used only once at this time, is it necessary to define a separate class for it? In this case, you can use an anonymous internal class to solve the problem.

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) {. printinfo () ;}} public class noinnerclassdemo02 {public static void main (string ARGs []) {new x (). fun1 (); // instantiate the X object and call the fun1 () method }}

Summary:
1. In actual Java Development, various development frameworks are often used, 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.