1. Use. This and. New
If you need to generate a reference to an external class object, you can use the name of the outer classes immediately following it. This
Public classInn {voidprint () {System.out.print ("Inn.print ()"); } Public classf1{ PublicInn dothis () {returnInn. This; } } PublicF1 F1 () {return NewF1 ();} Public Static voidMain (string[] args) {Inn inn=NewInn (); INN.F1 F1=inn.f1 (); F1.dothis (). print (); }}
A Inn class has a print () method, an inner class F1, and a F1 () method that returns the F1 object.
There is a dothis () method in the F1 class that will return the inn object.
The main method first instantiates a Inn Object Inn and then calls the F1 () method in the inn to generate a F1 object, and F1.dothis can get a inn object and invoke the Output method F () in it.
In the previous example, to obtain an object of an inner class F1, you must first create an object of the Outer Class Inn, because the inner class object is secretly connected to the object of the outer class, unless the static inner class is defined.
Then again, in the same example, to get an inner class F1 object, in addition to having to create an object of an external class inn, it is necessary to call the methods in the inn to get the F1 inner class object.
Public Static void Main (string[] args) { new Inn (); New Inn. F1 ();//This is wrong f1.dothis (). print ();
This compiles an error and you should use. New,
Public Static void Main (string[] args) { new Inn (); = Inn. New F1 (); F1.dothis (). print (); }
2. Anonymous inner class
In Java, the creation of an anonymous inner class requires an abstract class or interface to be implemented so that an abstract class or interface is created first
Public classInn {InterfaceF {inti = 11; intvalue (); } PublicF F () {return NewF () { Public intValue () {returni;} }; }
Public Static voidMain (string[] args) {Inn inn=NewInn (); F F=inn.f (); System.out.println (F.value ()); }}
There is an F interface and an F () method in the Inn class here, and the F () method combines the return value with the definition of the return value and the class that represents the return value, and then the class has no name.
This syntax is a simplified form of the following form
Public classInn {InterfaceF {inti = 11; intvalue (); } Public classF1Implementsf{ Public intValue () {returni;} } PublicF1 F () {return NewF1 ();} Public Static voidMain (string[] args) {Inn inn=NewInn (); F F=inn.f (); System.out.println (F.value ()); }}
A new class of F1 is created here, and this F1 class is just the anonymous inner class,
An anonymous inner class with parameters that implements:
Public classInn {Abstract classF { PublicFinti) {}Abstract intvalue (); } PublicF F (inti) { return NewF (i) { Public intValue () {returni;} }; } Public Static voidMain (string[] args) {inti = 2; Inn Inn=NewInn (); F F=INN.F (i); System.out.println (F.value ()); }}
Anonymous abstract class because there is no name, it does not have a constructor, its constructor is derived from its parent class, because the interface is not a constructor, so use abstract classes.
3. Nested classes
Declaring an inner class as static is often called a nested class, and an ordinary inner class object implicitly holds a reference to the outer class object that created it, but the inner class is static, so the nested class means:
A, to create a nested class, the object whose perimeter class is not required
b, non-static perimeter class objects cannot be accessed from objects in the nested class
C, the ordinary inner class fields and methods, can be placed on the external level of the class, so the ordinary inner class can not have static data and static fields, and can not contain nested classes, but nested classes can contain these things
D, nested classes can be placed in the interface as part of the interface, because any class of the interface is automatic public and static, because the class is static, just put the nested class in the interface namespace, you can even implement the external interface in the inner class.
4. If you create an inner class and then inherit its perimeter class and redefine this inner class:
Public classInn {PrivateF1 F; protected classf1{ PublicF1 () {System.out.println ("F1 ()"); } } PublicInn () {System.out.println ("Inn ()"); F=NewF1 (); }}classF2extendsinn{ Public classf1{ PublicF1 () {System.out.println ("F2 ()"); } } Public Static voidMain (string[] args) {NewF2 (); }}
The output is:
Inn () F1 ()
This example shows that when an outer class is inherited, there is no magical change in the inner class, and the inner class of the parent class and subclass is two completely separate entities, each in its own namespace.
Public classInn {PrivateF1 F; protected classf1{ PublicF1 () {System.out.println ("F1 ()"); } } PublicInn () {System.out.println ("Inn ()"); F=NewF1 (); }}classF2extendsinn{ Public classf1{ PublicF1 () {System.out.println ("F2 ()"); } } Public Static voidMain (string[] args) {F2 test1=NewF2 (); F2. F1 test2= Test1.NewF1 (); }}
The output of this is:
Inn () F1 () F2 ()
This is necessary if you want to explicitly inherit an inner class:
Public classInn {PrivateF1 F; protected classf1{ PublicF1 () {System.out.println ("F1 ()"); } } PublicInn () {System.out.println ("Inn ()"); F=NewF1 (); }}classF2extendsinn{ Public classF1extendsinn.f1{ PublicF1 () {System.out.println ("F2 ()"); } } PrivateF1 F; PublicF2 () {System.out.println ("F2 ()"); F1 F=NewF1 (); } Public Static voidMain (string[] args) {F2 test1=NewF2 (); F2. F1 test2= Test1.NewF1 (); }}
The output is:
Inn () F1 () F2 () F1 () F2 () F1 () F2 ()
The inner class of Java