Overview
Recently learning Python and found that Python supports more inheritance, which reminds me of this mechanism implemented by the internal class. This article is not about how to achieve more inheritance through the internal class, but to summarize the types and usage methods of the internal class.
Java internal class is divided into:
The internal class is used in the Android source code in large quantities. First, introduce the common points of these four internal classes:
Next, introduce these internal classes respectively.
Non -static internal class
When a class is a non -static member of another class, this class is a non -static internal class.
The example code of creating a non -static internal class is as follows:
class outClass {class innerClass {}}
When we compiled Javac, we found that two .class files were generated: OutClass.Class and OutClass $ InnerClass.Class. As shown in the figure below:
Internal classes from the non -static method of the external class
It is easy to access the internal class in the external class. Create the internal class object directly, and then call the method in the class by applying the object to the class. The example code is as follows:
Public class outClass {Private Static int a = 0; Public Void Makeinner () {InnerClass InClass = New InnerClass (); InclSS.Seeouter (); TATIC VOID Main (String [] Args) {OutClass OClass = New Outclass () ; Oclass.makeinner ();} Class InnerClass {Public Void Seeouter () {System.out.println (A); A ++;}}}}The results are as follows:
0
Internal classes from the static method of the external class
It is relatively simple to access the internal class in the external class. You can directly get the internal class objects, but if you want to use the internal class in the external class, you cannot directly NW's internal class name.
Outclass.innerClass innerClass = new outClass (). New InnerClass ();
In other words, the non -static internal class is required to be instantiated first, and then instantiated internal classes are used through the external class objects. The example code is as follows:
Public class outClass {Private Static int a = 0; Public Void Makeinner () {InnerClass InClass = New InnerClass (); InclSS.Seeouter (); TATIC VOID Main (String [] Args) {OutClass OClass = New Outclass () ; Outclass.innerClass InnerClass = New Outclass (). New InnerClass (); InnerClass.Seeouter ();} Class InnerClass {Public oid seeouter () {system.out.println (a); a ++;} }}
Run results:
01
THIS reference in the internal class
Ordinary classes can reference the current objects with this, as well as internal classes. But what if the internal class wants to quote the current object of the external class? You can use the following ways:
External name.this
The example code is as follows:
Public class outClass {Private Static int a = 0; Public Void Makeinner () {InnerClass InClass = New InnerClass (); InclSS.Seeouter (); TATIC VOID Main (String [] Args) {OutClass OClass = New Outclass () ; Outclass.innerClass InnerClass = New Outclass (). New InnerClass (); InnerClass.Seeouter ();} Class InnerClass {Public oad seeouter () {system.out.println (this); system.out .println (outClass.this);}}}
Static internal class
The non -static internal class is introduced above. Next, we learn that God Horse is a static internal class.
The static internal class is the role of a static member in the external class. The form of static internal class and the creation of non -static internal classes is very similar, but there is an additional static modifier in front of Class.
Note that the external class cannot be modified using the static modifier.
The example code is as follows:
class outClass {Static Class InnerClass {}}
Compile with the javac command, you can see that there are two .class files, as shown in the figure below:
Internal classes of static internal classes from the non -static method of the external class
Visiting static internal classes from external classes is the same as non -static internal classes in external categories. However, it should be noted that at this time, the static internal class can only access the static members of the external class, and the non -static members cannot be accessed.
The example code is as follows:
Public class outClass {Private Static int a = 0; Private int B = 1; Public void Makeinner () {InnerClass Inclss = New InnerClass (); inclas.seeou Ter ();} Public Static void main (string [] args) {outClass Oclass = new outClass (); oclass.makeinner ();} Static class innerClass {Public void seeouter () {system.out.println (this); system.out.printl n (a); // system.out.println ( b);}}}
The execution results are as follows:
OutClass $ InnerClass@79A3400
Internal classification of static internal classes from the external static method
Notice:
Because the static internal class is a static member of the external class, and the static members are bound to the class, not the object of instantiated objects. Therefore, internal classes in the static method of the external class do not need to instantiate the external class first.
The example code is as follows:
Public class outClass {Private Static int a = 0; Private int B = 1; Public void Makeinner () {InnerClass Inclss = New InnerClass (); inclas.seeou Ter ();} Public Static void main (string [] args) {outClass Oclass = new outClass (); oclass.makeinner (); outClass.InnerClass InClass = New Outclass.InnerClass (); Inclass.Seeououter ();} Static Class in Nerclass {Public Void Seeouter () {System.out.println (this) ; System.out.println (a); // System.out.println (b);}}}
Anonymous internal class
The anonymous internal class is flooding in the development of Android applications. Many of the implementation of various Listener objects are through anonymous internal classes.
The anonymous internal class can know that this is an internal class that does not have a category, which is usually used to simplify the code.
I believe that students who write Java use the threads. When Thread, we can pass a runnable object or an anonymous internal class. The example code is as follows:
Public class outClass {public void testanonymousClass () {thread T = new thread (new runnable () {@Override public void run () {for (int i = 0; i <10; I ++) {system.out.println (i); Try {Thread.sleep (500);} Catch (InterruptedException E) {e.printstacktrace ();}}); t.start (); System.out.println ("Another Thread I s running. .. ");} Public Static void main (string [] args) {outClass Oclass = new outClass (); oclass.testanonymousClass ();}}
The execution results are as follows:
another thread is running ...