I. Basic Syntax of internal classes
1. The top-level class can only be at the public and default access levels, while the member Internal classes can be at the public, protected, default, and private access levels.
2. The instance internal class has the following features:
L when creating an instance's internal class instance, the instance of the external class must already exist.
L instances of the internal class of the instance automatically hold the reference of the instance of the external class. In the internal class, you can directly access all members of the external class, including member variables and member methods. In addition, the internal class can access members of all external classes in multiple nesting.
L there is a one-to-multiple relationship between an external class instance and an internal class instance. An internal class instance will only reference one external class instance, an external class instance corresponds to zero or multiple internal class instances. External classes cannot directly access internal class instances. They must be accessed through internal class instances.
L static members cannot be defined in the instance's internal class, but only instance members can be defined.
L if the instance's internal class B and external Class A contain members with the same name (such as the member variable V), then in Class B, this. V indicates a member of Class B. this. V indicates a member of Class.
3. static internal classes have the following features:
L instances of static internal classes do not automatically hold references to specific instances of external classes. When creating instances of internal classes, you do not have to create instances of external classes.
L The static internal class can directly access the static members of the external class. If you access the instance members of the external class, you must access it through the instance of the external class.
L static members and instance members can be defined in static internal classes.
L The customer class can directly access the static members of the static internal class through the complete class name.
4. A local internal class is an internal class defined in the method. Its visible range is the current method. It is the same as a local variable, the access control modifier and static modifier cannot be used for local internal classes. Local internal classes have the following features:
L local internal classes can only be used in the current method.
L The local internal class is the same as the instance internal class and cannot contain static members.
L internal classes defined in local internal classes cannot be modified by public, protected, or private access control modifiers.
L The local internal class is the same as the instance internal class. It can access all members of the external class. In addition, the local internal class can also access final type parameters and variables in the method. You cannot access local variables not modified by final.
Ii. Inheritance of internal classes
When a Class A inherits the internal Class C of an external Class B, it is required that the constructor of Class A pass the reference of an instance of Class B through parameters, then, call the super statement in the constructor to establish the association between Class A instance and Class B instance. ExampleCodeAs follows:
|
ClassB {
PublicB (){}
ClassC {
PublicC (){}
}
}
Public Class A ExtendsB .C {
PublicA (B ){
B.Super();
}
Public Static VoidMain (string [] ARGs ){
B =NewB ();
A A =NewA (B );
}
} |
Iii. Anonymous class
|
Public Class A{
A (IntV ){
System.Out. Println ("another constructor ");
}
A (){
System.Out. Println ("default constructor ");
}
VoidMethod (){
System.Out. Println ("from ");
}
Public Static VoidMain (){
A A =NewA (){//Anonymous class
VoidMethod (){
System.Out. Println ("From anonymous");
}
};
A. Method ();
}
}
// The above "new (){...}" Defines an anonymous class that inherits Class A. the braces are the class bodies of Class A, "new (){...}" Return
// Reference an instance of the anonymous class.
// Print the result as follows:
Default constructor
From
Default constructor
From anonymous
|
1. Anonymous classes have the following features:
L The Anonymous class does not have a constructor, but the constructor of the parent class is called. The sample code is as follows:
|
Public Static Void Main (){
IntV = 1;
A A =NewA (V) {// Anonymous class
VoidMethod (){
System. Out. println ("from anonymous ");
}
};
A. Method ();
}
// Print the following code:
Another Constructor
From anonymous |
In the above "New a (v ){...}" If the parameter V is a local variable and is used in the class body of the anonymous class, it must be of the final type; otherwise, compilation errors may occur.
|
Public Static Void Main (){
IntV = 1; // compilation error. V must be defined as final.
A A =NewA (V) {// Anonymous class
VoidMethod (){
System. Out. println ("from anonymous" +V); // Use the local variable v
}
};
A. Method ();
} |
L although the Anonymous class does not have a constructor method, you can provide an instance initialization code in the anonymous class. the Java Virtual Machine will execute this code after calling the constructor of the parent class. Example:
|
Public Static Void Main (){
IntV = 1;
A A =NewA (V) {// Anonymous class
{//Instance initialization code---Start
System. Out. println ("Initialize instance");
}//Instance initialization code---End
VoidMethod (){
System. Out. println ("from anonymous ");
}
};
A. Method ();
}
// Print the result as follows:
Another Constructor
Initialize instance
From anonymous |
Note: An anonymous instance can only be initialized in one way.
L apart from defining anonymous classes in external class methods, you can also define an anonymous class when declaring a member variable. The sample code is as follows: Class A has an instance variable A, which references an instance that inherits an anonymous class of Class.
|
Abstract ClassA {
A A =NewA (){
VoidMethod (){
System.Out. Println ("inner ");
}
};
Abstract VoidMethod ();
} |
L in addition to inheriting classes, anonymous classes can also implement interfaces. The sample code is as follows:
|
Public Class Sample{
Public Static VoidMain (){
Thread t =NewThread (NewRunnable (){
Public VoidRun (){
System.Out. Println ("Hi");
}
});
T. Start ();
}
} |
L like a local internal class, an anonymous class can access all members of an external class. If an anonymous class is located in a method, it can also access final type variables and parameters of the method.
L The name of a local internal class is invisible outside the method. Therefore, like an anonymous class, it can encapsulate the type name. The local internal class and Anonymous class have the following differences:
Ø Anonymous class andProgramThe code is short.
A local internal class can have multiple overload constructor methods, and the customer class can create local internal class instances multiple times. However, the anonymous class does not overload the constructor and can only create an instance once.
Iv. Internal classes in internal interfaces and interfaces
L internal interfaces can also be defined in a class.
L you can define static internal classes in the interface. In this case, static internal classes are located in the interface namespace.
5. Internal class files
For each internal class, the Java compiler generates an independent. Class file. The naming rules for such files are as follows:
L Member internal class: External class name $ internal class name
L local internal class: name of the external class $ number $ name of the internal class
L Anonymous class: name of the external class $ number