Inner class: When describing a thing, the inner class of a thing has a transaction, which is described by an inner class.
Access rules for internal classes:
1, the inner class can access the members of the external class directly, including the private,.
Members in an external class can be accessed directly because the inner class holds a reference to an external class, in the form of an external class name. This
2, the outer class must establish an inner class object to access the inner class.
Access format:
1, when the inner class is defined on the member position of the outer class, and is not private, it can be in an external other class,
You can create an inner class object directly.
Format:
The outer class name. internal Class Name Object = External Class object. Inner Class object
Outer.Inner in = new outer (). New Inner ();
2, when the inner class is in the member position, it can be modified by the member modifier,
For example, private: Encapsulate inner classes in the outer class
Static: The inner class has static properties.
When an inner class is modified by static, only static members in the outer class can be accessed directly. Access limitations have occurred
In other classes of external classes, how do you directly access the non-static members of the static inner class?
New Outer.Inner (). function ();
In other external classes, how do you directly access static members of static internal classes?
Outer.inner.function ();
Note: When a static member is defined in the inner class, the inner class must be static,
When a static method in an external class accesses an inner class, the inner class must also be static.
classouter{intX=3; Static classInter {Static voidfunction () {SYSTEM.OUT.PRINTLN ("Inner;" +outer. This. x); } } voidMethod () {Inter in=NewInter; In.function (); }}classTest { Public Static voidMain (string[] args) {outer out=Newouter (); Out.method (); //direct access to members within the inner class;//Outer.Inner in = new outer (). New Inner (); //in.function (); }}
DAY09 Inner class