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