Inner class : Defines a class within another class, which is called an inner class.
Class Outer { private int num = 3; Class Inner//It wants to access num in outer, if you need to create an object outside, and then access it through the exposed interface, but now the outer inside, you can directly access it. So convenient { void Show () { System.out.println ("Show run ..." + num); } } If you want to access Inner now, you need this public void method () { Inner in = new Inner (); In.show (); } Internal classes are internally defined as members, so you can use member modifiers, such as: //private class test{} static class Inner { static void function () { System.out.println ("function run ..." +num);} }}
Internal class Access features:
1. The inner class can access members in the external class directly.
2. External classes to access the inner class, you must establish an object for the inner class.
Generally used for the design of classes.
When analyzing a thing, it is found that there is something in the description of the thing, and that the thing is still accessing the content of the described thing.
At this point, the other things are defined as internal classes to describe.
Class Innerclassdemo {public static void Main (string[] args) { Outer out = new Outer (); Out.method (); Direct access to members in an inner class in an external class. Outer.Inner in = new Oouter (). New Inner (); In.show (); If the inner class is static, it is equivalent to an external class. Outer.Inner in = new Outer.Inner (); In.show (); If the inner class is static, the member is static. Outer.Inner.function (); If a static member is defined in an inner class, the inner class must also be static. } }
Details
Class Outer { int num = 3; Class Inner { int num = 4; void Show () { int num = 5; SYSTEM.OUT.PRINTLN (num);//5 System.out.println (this.num);//4 System.out.println (Outer.this.num);//3 } } void method () { new Inner (). Show (); } } Class InnerClassDemo2 {public static void Main (string[] args) { new Outer (). method (); } }
why does an inner class have direct access to members of an external class?
That's because the inner class holds references to external classes-----external class names. This
Internal classes can be stored in local locations
Class Outer { int num = 3; Object method () { final int x = 9; Class Inner { void Show () { System.out.println ("show ..." +num);} } Object in = new Inner (); Return in;//0x0045 } } class InnerClassDemo3 {public static void Main (string[] args) { Outer out = new Outer (); Object obj = Out.method ();//0x0045 } }
The inner class can only access local variables that are final decorated locally in local locations.
The anonymous inner class is the shorthand format for the inner class.
Must have a premise:
An inner class must inherit or implement an external class or interface.
Anonymous inner class: is actually an anonymous subclass object.
Format: New parent class or interface () {Subclass content}
Abstract class Demo { abstract void Show (); } Class Outer { int num = 4; Class Inner extends Demo//This is normal condition { void Show () { System.out.println ("show ...." +num);} } Public void Method () { new Demo ()//This is called anonymous inner class { void Show () { System.out.println ("show ...." +num); }}} }
Application of anonymous inner class
Interface Inter { void Show1 (); void Show2 (); } Class Outer {public void method () { new Inter () {public void Show1 () {} Public void Show2 () {} }.show1 (); The Show1 method is called, which is equivalent to the new Inner (). Show1 (); Or use the following method Inter in = new Inter () {public void Show1 () {} public void Show2 () {} } In.show1 (); In.show2 (); } }
one of the usual usage scenarios:
When a function parameter is an interface type, and there are no more than three methods in the interface.
The anonymous inner class can be passed as an actual parameter.
Class InnerClassDemo5 {public static void Main (string[] args) { Show (new Interimpl ()); } public static void Show (Inter in) { in.show1 (); In.show2 (); } } Class Interimpl implements Inter {}
Initialization of an object
Class Fu { int num = 9; { System.out.println ("Fu"); } Fu () { super (); Show initialize //construct code block initialize show (); void Show () { System.out.println ("Fu show ..." +num); Overridden, running subclass } } class Zi extends Fu { int num = 8; { System.out.println ("Zi"); } Zi () { super (); Show initialize //construct code block initialize show (); void Show () { System.out.println ("Zi show ..." +num);} Class Inner {public static void Main (string[] args) { new Zi (); } }
/*
Fu
Zi show ... 0
Zi
Zi show ... 8
*/
Java Learning personal memo within a class