Java anonymous internal class, java Anonymous

Source: Internet
Author: User

Java anonymous internal class, java Anonymous

/*
Anonymous internal class:
1. The anonymous internal class is in short format.
2. Prerequisites for defining anonymous internal classes:
An internal class must inherit a class or implement an interface.
3. Format of the anonymous internal class: new parent class or interface () {defines the content of the subclass}
4. In fact, an anonymous internal class is an anonymous subclass object. And the object is a little fat. It can be understood as an object with content.
5. It is recommended that no more than three methods are defined in the anonymous internal class.


*/

Abstract class AbsDemo {abstract void show ();} class Outer {int x = 3;/* class Inner extends AbsDemo {int num = 90; void show () {System. out. println ("show:" + num);} void abc () {System. out. println ("hehe") ;}} */public void function () {// AbsDemo a = new Inner (); // Inner in = new Inner (); // in. show (); // in. abc (); AbsDemo d = new AbsDemo () {int num = 9; void show () {System. out. println ("num =" + num);} void abc () {System. out. println ("haha") ;}}; d. show (); // d. abc (); // compilation failed;} class InnerClassDemo4 {public static void main (String [] args) {new Outer (). function ();}}

 

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.