Java Anonymous inner class

Source: Internet
Author: User

Anonymous inner class:

1. The class is in the interior of a class;

2, the class has no name, only the structure of the class body;

Demo Process:

1. Interface A

1 Interface a{2  Fooa ();   3 }

2. The method of Class B requires an object of type A as a parameter

1 class b{2  Foob (a a) {3       System.out.println ("Foob"); 4       A.fooa ();             5    }  6 }

3, test class, there is no need to have a class to implement interface A, using anonymous internal classes can be;

1 classtest{2      Public Static voidMain (string[] args) {3b b =NewB ();4B.foob (NewA () {5 Fooa () {6System.out.println ("Fooa");7 }8 });9 }Ten}

Java Anonymous inner class

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.