Overview
The recent study of Python, discovering that Python supports multiple inheritance, reminds me that Java is the set of mechanisms that are implemented through internal classes. This article is not about how to implement multiple inheritance through inner classes, but to summarize the types and usage of internal classes.
The Java inner classes are divided into:
- Non-static inner class
- Static Inner class
- Local inner class
- Anonymous inner class
Internal classes are used in a lot of the Android source code to first introduce the common features of these four kinds of internal classes:
- The inner class is still a separate class, and after compilation The inner class is compiled into a separate. class file, but preceded by the class name and the $ symbol of the outer class.
- Internal classes cannot be accessed in a normal way. An inner class is a member of an external class, because an inner class is free to access members of the outer class, including private members.
- The inner class is declared static and cannot arbitrarily access the member variable of the external class, at which point the inner class can only access static member variables of the external class.
Next, introduce each of these kinds of internal classes.
non-static inner class
When a class is a non-static member of another class, the class is a non-static inner class.
The sample code for creating a Non-static inner class is as follows:
Class Outclass {
class Innerclass {}
}
When we compiled with Javac, we found that two. class files were generated: Outclass.class and Outclass$innerclass.class. As shown in the following illustration:
To instantiate an inner class from a non-static method of an external class
It is easy to access an inner class in an external class, directly creating an inner class object and then invoking the method within the class through the object instance. The sample code is as follows:
public class Outclass {
private static int a = 0;
public void Makeinner () {
Innerclass inclass = new Innerclass ();
Inclass.seeouter ();
}
public static void Main (string[] args) {
outclass oclass = new Outclass ();
Oclass.makeinner ();
}
Class Innerclass {public
void Seeouter () {
System.out.println (a);
a++
}}
}
The results of the operation are as follows:
To instantiate an inner class from a static method of an external class
Accessing an inner class in an external class is relatively straightforward and can be directly new to the inner class object, but if you want to use the inner class outside of the outer class, then you can't directly new the inner class name, but you need the following way:
Outclass.innerclass innerclass = new Outclass (). New Innerclass ();
That is, to invoke a non-static inner class externally, you need to instantiate the classes first, and then instantiate the inner class with the Outer class object. The sample code is as follows:
public class Outclass {
private static int a = 0;
public void Makeinner () {
Innerclass inclass = new Innerclass ();
Inclass.seeouter ();
}
public static void Main (string[] args) {
outclass oclass = new Outclass ();
Oclass.makeinner ();
Outclass.innerclass innerclass = new Outclass (). New Innerclass ();
Innerclass.seeouter ();
}
Class Innerclass {public
void Seeouter () {
System.out.println (a);
a++
}}
}
Run Result:
This reference for the inner class
Ordinary classes can use this to refer to the current object, as is the inner class. But what if the inner class wants to refer to the current object of the outer class? You can use the following methods:
The external class name. This
The sample code is as follows:
public class Outclass {
private static int a = 0;
public void Makeinner () {
Innerclass inclass = new Innerclass ();
Inclass.seeouter ();
}
public static void Main (string[] args) {
outclass oclass = new Outclass ();
Oclass.makeinner ();
Outclass.innerclass innerclass = new Outclass (). New Innerclass ();
Innerclass.seeouter ();
}
Class Innerclass {public
void Seeouter () {
System.out.println (this);
System.out.println (Outclass.this);}}
Static Inner class
The above describes the non-static inner class, and then we learn that the God horse is a static inner class.
A static inner class is the role of a static member in an external class, and creating a static inner class is similar to creating a non-static inner class, except that the class has a static modifier in front of it.
Note that external classes cannot be decorated with the static modifier.
The sample code is as follows:
Class Outclass {
static class Innerclass {
}
}
Compiled with the Javac command, you can see that there are two. class files, as shown in the following illustration:
To instantiate a static inner class from a non-static method of an external class
Accessing a static inner class from an external class is the same as accessing a non-static inner class in the outer classes. However, it is important to note that static internal classes can only access static members of external classes and cannot access non-static members.
The sample code is as follows:
public class Outclass {
private static int a = 0;
private int b = 1;
public void Makeinner () {
Innerclass inclass = new Innerclass ();
Inclass.seeouter ();
}
public static void Main (string[] args) {
outclass oclass = new Outclass ();
Oclass.makeinner ();
}
Static class Innerclass {public
void Seeouter () {
System.out.println (this);
System.out.println (a);
System.out.println (b);
}
}
The results of the implementation are as follows:
outclass$innerclass@79a340
0
To instantiate a static inner class from a static method of an external class
Attention:
Because the static inner class is a static member of the outer class, the static member is bound to the class, not to the object instantiated by the class. Therefore, it is not necessary to instantiate an inner class in a static method of an external class without first instantiating it.
The sample code is as follows:
public class Outclass {
private static int a = 0;
private int b = 1;
public void Makeinner () {
Innerclass inclass = new Innerclass ();
Inclass.seeouter ();
}
public static void Main (string[] args) {
outclass oclass = new Outclass ();
Oclass.makeinner ();
Outclass.innerclass Inclass = new Outclass.innerclass ();
Inclass.seeouter ();
}
Static class Innerclass {public
void Seeouter () {
System.out.println (this);
System.out.println (a);
System.out.println (b);
}
}
Anonymous inner class
Anonymous inner classes are rampant in Android application development, and many of the listener objects are implemented through anonymous inner classes.
Anonymous inner classes can be known by name to represent an inner class with no class name, and are often used to simplify the code.
Believe that write Java students have used threads, that thread when we can pass a Runnable object, can also pass an anonymous inner class. The sample 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 < i + +) {
System.out.println (i);
try {
thread.sleep;
} catch (Interruptedexception e) {
e.printstacktrace ()
}}}} );
T.start ();
System.out.println ("Another thread is running ...");
}
public static void Main (string[] args) {
outclass oclass = new Outclass ();
Oclass.testanonymousclass ();
}
The results of the implementation are as follows:
Another thread is running ...