There are few internal departments and classes, which are basically unfamiliar. Today, when I saw a blog, I thought it was pretty good. Thank you, blogger !!!
1. Description of internal classes
1) define another class in a class. This class is called an inner class ). The definition of an internal class is no different from that of a common class. It can directly access and reference all variables and methods of its external class (including private ), just like other non-static members in an external class. The difference is that external classes can only be declared as public and default, while internal classes can be declared as private and protected. Www.zxbc.cn
2) When we create an inner class, the object has a relationship with the external class object, which is formed through a special this reference, when the member methods of an internal class access a variable/method, if this variable is not defined in this method and the internal class, the call will be passed to the reference (outerclass. this), through the reference of the external class object to call this variable.
2. Internal class variable access
2.1 calling internal class methods directly from external classes in main
Package org. ZJ. sample;
Class outer {
Private int indexed = 100;
Class inner {
Private int Index = 50;
Void print (){
Int Index = 30;
System. Out. println (INDEX); // 30
System. Out. println (this. Index); // 50
System. Out. println (outer. This. Index); // 100
}
}
Void print (){
Inner inner = new inner (); // get internal class reference
Inner. Print ();
}
}
Class test {
Public static void main (string [] ARGs ){
Outer outer = new outer ();
Outer. Print ();
}
}
The example program lists the distribution of variable names for duplicate members.
Access the variables in the internal class method:
System. Out. println (INDEX );
Access the member variables in the internal class:
System. Out. println (this. Index );
Access the member variables of the external class:
System. Out. println (outer. This. Index );
2.2 explicitly returning internal class references in main
Package org. ZJ. example;
Class outer {
Private int indexed = 100;
Class inner {
Private int Index = 50;
Void print (){
Int Index = 30;
System. Out. println (INDEX); // 30
System. Out. println (this. Index); // 50
System. Out. println (outer. This. Index); // 100
}
}
Inner getinner (){
Return new inner (); // returns a reference to an internal class.
}
}
Class test {
Public static void main (string [] ARGs ){
Outer outer = new outer ();
Outer. Inner inner = outer. getinner ();
Inner. Print ();
}
}
2.3 when the main method is inside the outer class
Package org. ZJ. sample1;
Class outer {
Private int indexed = 100;
Class inner {
Private int Index = 50;
Void print (){
Int Index = 30;
System. Out. println (INDEX); // 30
System. Out. println (this. Index); // 50
System. out. println (Outer. this. index); // 100
}
}
Inner getInner (){
Return new Inner ();
}
Public static void main (String [] args ){
Outer outer = new Outer ();
Inner inner = outer. getInner (); // note the changes here.
Inner. print ();
}
}
2.4 directly generate internal class objects in the main method
Package org. zj. sample2;
Class Outer {
Private int indexed = 100;
Class Inner {
Private int index = 50;
Void print (){
Int index = 30;
System. out. println (index); // 30
System. out. println (this. index); // 50
System. out. println (Outer. this. index); // 100
}
}
}
Class Test {
Public static void main (String [] args ){
Outer outer = new outer ();
Outer. Inner inner = outer. New inner (); // note the changes here.
Inner. Print ();
}
}
2.5 define internal classes in methods
The internal class defined in the method can only access local variables of the final type in the method, because the local variables defined with final are equivalent to a constant, and its life cycle exceeds the life cycle of the method running. The internal classes in the method body cannot have access specifiers.
Package org. ZJ. sample;
Class inout {
String STR = new string ("");
Public void amethod (final int writable GS ){
Class inner {
Public inner (){
System. out. println ("This is Inner."); // you can see that it is different from anonymous internal class usage.
}
Public void sayHello (){
System. out. println (str );
System. out. println (Bytes GS );
}
}
Inner inner = new Inner (); // This sentence must be placed behind the Inner of the definition class.
Inner. sayHello ();
}
Public static void main (String [] args ){
InOut inOut = new InOut ();
InOut. amethod (33 );
}
}
You can use an internal class in the method body to use a named constructor or to overload the constructor. The anonymous internal class can only be used for instance initialization.
3. Inheritance of internal classes
When a class inherits from an internal class, the default constructor is unavailable. The following syntax must be used:
EnclosingClassReference. super (); (see the green section in the following example)
Package org. zj. sample;
Class WithInner {
Class Inner {
Public void sayHello (){
System. out. println ("Hello .");
}
}
}
Public class InheritInner extends WithInner. Inner {
InheritInner (WithInner wi ){
Wi. super ();
}
Public static void main (String [] args ){
WithInner wi = new WithInner ();
InheritInner ii = new InheritInner (wi );
Ii. sayHello ();
}
}
Result:
Hello.
4. Anonymous internal class
If anonymous internal classes are not used: an internal class Inner is written here.
Package org. zj. example;
Abstract class {
Abstract public void sayHello ();
}
Class Outer {
Public static void main (String [] args ){
Class Inner extends {
Public void sayHello (){
System. out. println ("Hello! ");
}
}
New Outer (). callInner (new Inner ());
}
Public void callInner (A ){
A. sayHello ();
}
}
When anonymous internal classes are used: the Inner definition is provided directly in new.
Package org. zj. example;
Abstract class {
Abstract public void sayHello ();
}
Class Outer {
Public static void main (String [] args ){
New Outer (). callInner (new (){
Public void sayHello (){
System. out. println ("Hello! ");
}
});
}
Public void callInner (A ){
A. sayHello ();
}
}
5. Two special usage of internal classes
A class derives from another class and implements an interface. But the methods defined in the interface have different meanings from those defined in the parent class, you can use the internal class to solve this problem.
Package org. zj. sample;
Interface Machine {
Void run ();
}
Class Person {
Void run (){
System. out. println ("run ");
}
}
Class Robot extends Person {
Private class MachineHeart implements Machine {
Public void run (){
System. out. println ("heart run ");
}
}
Machine getMachine (){
Return new MachineHeart ();
}
}
Class Test {
Public static void main (String [] args ){
Robot robot = new Robot ();
Machine m = robot. getMachine ();
M. run ();
Robot. run ();
}
}
In the Robot class, use the internal MachineHeart class to implement the run method of the interface Machine. At the same time, the Robot class inherits the run method of the parent class Person. If the Robot directly implements the interface Machine without using the internal MachineHeart class, how can I call the run method of the parent class?
Internal classes can be used to solve the problem of multiple inheritance in c ++.
Package org. ZJ. example;
Class {
Void fn1 (){
System. Out. println ("It's fn1 .");
}
}
Abstract class B {
Abstract void FN2 ();
}
Class C extends {
B getb (){
Return new B (){
Public void FN2 (){
System. Out. println ("It's fn2 .");
}
};
}
}
Class Test {
Public static void main (String [] args ){
C c = new C ();
C. fn1 ();
C. getB (). fn2 ();
}
}
Class C should inherit both Class A and Class B, then it can put the definition of Class B inside class C to make it an internal class.
6. Why should we use internal classes?
This is a summary of the two usage methods in the previous chapter, which can be analyzed based on the two examples in the previous chapter.
1) when we need to implement an interface in a certain situation, and do not need to implement this interface in another situation, we can use internal classes to solve this problem. Let Internal classes implement this interface.
2) The internal class effectively solves the problem of multiple inheritance.
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/armyjian/archive/2007/11/05/1867936.aspx