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 the
A reference to an external class, in the form of an external class name.
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 other classes of the outer class.
You can create an inner class object directly.
Format
The outer class name. Internal class Name Variable name = 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 decorated by the member modifier.
For example, private: Inner classes are encapsulated 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 restrictions have occurred.
In other classes in the outer class, how do you directly access the non-static members of the static inner class?
New Outer.Inner (). function ();
In other classes of external classes, how do you directly access static members of static internal classes?
Outer.Inner.function ();
Note: When a static member is defined in an inner class, the inner class must also be static.
When a static method in an external class accesses an inner class, the inner class must also be static.
When describing a thing, there is something inside the thing, which is described by an inner class.
Because internal things are using the content of external things.
Class Body
{
Private Class Heart
{
}
public void Show ()
{
New Hear ()
}
}
classouter{intX= 3; classInner {intX= 2; voidfunction () {intx = 4; System.out.println ("Inner:" +outer. This. x); } } voidmethod () {Inner in=NewInner (); In.function (); }} Public classInnerclassdemo { Public Static voidMain (string[] args) {Outer out=NewOuter (); Out.method (); //directly accesses members in the inner class. //Outer.Inner in = new Outer (). New Inner ()//inch }}
When the inner class is defined locally,
1, can not be decorated by member modifiers.
2, you can access members in an external class directly, because you also hold references in external classes.
However, you cannot access the variables in the local area where it resides. Only the final decorated local variables can be accessed.
classouter{intx = 3; voidmethod () {inty = 4; classInner {voidfunction () {System.out.println (y); } } NewInner (). function (); }}classinnerclassdemo3{ Public Static voidMain (string[] args) {NewOuter (). method (); }}
1, the anonymous inner class is actually the shorthand format for the inner class.
2, the premise of defining an anonymous inner class:
The inner class must inherit a class or implement an interface.
3, Anonymous inner class format: New parent class or interface () {Defines the subclass's class tolerance}
4, in fact, the anonymous inner class is an anonymous subclass object. And the object is a little fat.
You can also be understood as an object with content.
5, the methods in the anonymous inner class are generally no more than 2.
Abstract classabsdemo{Abstract voidshow ();}classouter{intx = 3; /*class Inner extends Absdemo {void Show () {System.out.println ("method:" +x); } } */ Public voidfunction () {//new Inner (). Show (); NewAbsdemo () {voidShow () {System.out.println ("Method:" +x); }}.show (); }}classinnerclassdemo4{ Public Static voidMain (string[] args) {NewOuter (). function (); }}
Interfaceinter{voidmethod ();}classtest{/*Static class Inner implements Inter {public void method () {System.out.println ("method== ="); } } */ StaticInter function () {return NewInter ()//complements the code through anonymous inner classes. { Public voidmethod () {System.out.println ("method==="); } }; }}classinnerclasstest{ Public Static voidMain (string[] args) {//there is a static method function in the Test.function:Test class. //. Method (): Function The result of this method operation is an object. And is an object of type Inter. //method methods can be called because only objects of type Inter are available. test.function (). method (); }}
Java Learning--internal classes