1. Basic internal knowledge:
Generally, classes defined in the java class become internal classes.
Internal classes can be divided into: class defined outside the method body, class defined inside the method, static internal class (can only be defined outside the method), anonymous internal class
Note:
Classes defined outside the method:
The member variables (static and non-static) of the class can be accessed. To ensure that the member variables of the class can be correctly referenced, the object of the external class must be instantiated first, to instantiate an internal class object.
The access permission can be anything. You can think of it as a member variable of the class. This is a lot better to understand.
Classes defined in the method body;
The member variables (static and non-static) of the class can be accessed. To ensure that the member variables of the class can be correctly referenced, the object of the external class must be instantiated first, to instantiate an internal class object.
The access permission cannot be available. You can regard it as a local variable of the method.
Static internal class:
Only static member variables of the category class can be used.
Access permission any
Anonymous internal class:
The member variables (static and non-static) of the class can be accessed. To ensure that the member variables of the class can be correctly referenced, the object of the external class must be instantiated first, to instantiate an internal class object.
The access permission cannot be
2. Functions of internal classes
The internal class can hide the class. Generally, the class does not allow private protect default access.
Internal classes can implement multiple foundations to make up for the characteristics that java cannot inherit more.
3. Example
[Java]
Package com. ajun. test. innerclass. example;
/**
* Fruit content
* @ Author Administrator
*
*/
Public interface Contents {
String value ();
}
[Java]
Package com. ajun. test. innerclass. example;
/**
* Fruit destination
* @ Author Administrator
*
*/
Public interface Destination {
// Destination
String readLabel ();
}
[Java]
Package com. ajun. test. innerclass. example;
Public class Goods {
Private String des = "is ruit !! ";
// Method External
Private class Content implements Contents {
Private String name = "apple" + des;
@ Override
Public String value (){
Return name;
}
}
// Method External
Private class GDestination implements Destination {
Private String label;
Private GDestination (String label ){
This. label = label;
}
@ Override
Public String readLabel (){
Return label;
}
}
// Anonymous internal class
Public Destination getdestination (final String label ){
Return new Destination (){
@ Override
Public String readLabel (){
Return label;
}
};
}
Public Destination dest (String s ){
Return new GDestination (s );
}
Public Contents content (){
Return new Content ();
}
Public Destination dest2 (String s ){
Class GDestination implements Destination {
Private String label;
Private GDestination (String label ){
This. label = label;
}
@ Override
Public String readLabel (){
Return label;
}
}
Return new GDestination (s );
}
}
[Java]
Package com. ajun. test. innerclass. example;
Public class Test {
Public static void main (String [] ){
Goods gs = new Goods ();
Contents c = gs. content ();
Destination d = gs. dest ("Beijing ");
System. out. println (c. value ());
System. out. println (d. readLabel ());
Destination d1 = gs. getdestination ("Shanghai ");
System. out. println (d1.readLabel ());
System. out. println (gs. dest2 ("Tianjin"). readLabel ());
}
}
Content and Gdestination are well hidden. When calling outside, you do not know which class is called, so that this class has multiple inheritance features.
Output;
[Plain]
Apple is ruit !!
Beijing
Shanghai
Tianjin