Java learning within class, anonymous inner classes

Source: Internet
Author: User
Tags local time

Internal classes are defined at local time:

1. Can not be modified by member modifiers

2. Members in an external class can be accessed directly because they also hold references in the outer class, but cannot access the variables in the local area where they are located, and can only access the final decorated local variables

 Packagecom.dreamy.day04;/** * @authorDreamy **/classouter{intX=3; voidMethodFinal inta) {Final intY=4; classinner{voidfunction () {System.out.println (a); }        }        NewInner (). function (); }} Public classInnerClassDemo01 {/**     * @paramargs*/     Public Static voidMain (string[] args) {Outer out=NewOuter (); Out.method (7); Out.method (8); }}

Anonymous inner class:

1. The anonymous inner class is actually the shorthand format for the inner class.

2. Prerequisites for defining anonymous internal classes:

The inner class must inherit a class or implement an interface.

3. The format of the anonymous inner class: New parent class or interface () {Defines the contents of the subclass};

4. In fact, the anonymous inner class is an anonymous subclass object. And the object is a little fat. Can be understood as an object with content.

 Packagecom.dreamy.day04;/** * @authorDreamy **/Abstract classabsdemo{Abstract voidshow ();}classouter{intX=3; /*class Inner extends absdemo{void Show () {System.out.println ("show:" +x); }    }    */     Public voidfunction () {//new Inner (). Show ();        /*anonymous inner class 1:new Absdemo () {void Show () {System.out.println ("show:" +x);        }        }; */                //Anonymous Inner Class 2://new Inner (). Show ();        NewAbsdemo () {@OverridevoidShow () {System.out.println ("x===" +x);    }}.show (); }} Public classInnerClassDemo01 {/**     * @paramargs*/     Public Static voidMain (string[] args) {NewOuter (). function (); }}

5. It is best not to have more than 3 methods defined in an anonymous inner class

 Packagecom.dreamy.day04;/** * @authorDreamy **/Abstract classabsdemo{Abstract voidshow ();}classouter{intX=3; /*class Inner extends absdemo{void Show () {System.out.println ("show:" +x); }    }    */     Public voidfunction () {//new Inner (). Show ();        /*anonymous inner class 1:new Absdemo () {void Show () {System.out.println ("show:" +x);            } void ABC () {System.out.println ("haha");        }        }; */                //Anonymous Inner Class 2://new Inner (). Show ();        NewAbsdemo () {@OverridevoidShow () {System.out.println ("x===" +x); }            voidABC () {System.out.println ("Haha");    }}.abc (); }} Public classInnerClassDemo01 {/**     * @paramargs*/     Public Static voidMain (string[] args) {//new Outer (). function (); //Absdemo a=new Inner ();Absdemo d=NewAbsdemo () {@OverridevoidShow () {System.out.println ("x===" +x); }            voidABC () {System.out.println ("Haha");        }        };        D.show (); //d.abc ();/compilation failed because the parent class does not have this method.     }}
 Packagecom.dreamy.day04;/** * @authorDreamy **/Abstract classabsdemo{Abstract voidshow ();}classouter{intX=3;  Public voidfunction () {//new Inner (). Show ();        /*anonymous inner class 1:new Absdemo () {int num=9;            void Show () {System.out.println ("num:" +num);            } void ABC () {System.out.println ("haha");        }        }; */                //Anonymous Inner Class 2://new Inner (). Show ();        NewAbsdemo () {intNum=90; @OverridevoidShow () {System.out.println ("x===" +num); }            voidABC () {System.out.println ("Haha");    }}.show (); }} Public classInnerClassDemo01 {/**     * @paramargs*/     Public Static voidMain (string[] args) {NewOuter (). function (); //Absdemo a=new Inner ();Absdemo d=NewAbsdemo () {intNum=9; @OverridevoidShow () {System.out.println ("num===" +num); }            voidABC () {System.out.println ("Haha");        }        };        D.show (); //d.abc ();/compilation failed because the parent class does not have this method.     }}

Java learning within class, anonymous inner classes

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.