The function of the inner class:
1, hide the internal realization, high cohesion.
2, Java Multi-inheritance implementation.
What is the multiple inheritance of Java?
Java only supports single inheritance, so if multiple inheritance is required, then internal classes can be implemented.
How is it implemented?
1. Parent Class A
public class A {
public int Age () {
return 1;
}
}
2. Parent Class B
public class B {
Public String name () {
Return "Zhang San";
}
}
3, the inner class contains class C
public class C extends otherdefclass{
Private class EA extends a {///Inherits Class A, optionally overriding the parent class method.
public int Age () {
Return super () +1;
}
}
Private class EB extends B {//Inherit class B
Public String name () {
Return super () + "EB";
}
}
public int Age () {
return new EA (). age (); Add the extra processing you need
}
Public String name () {
return new EB (). Name ();
}
}
At this point, call the age method of Class C or the name method, and you can call the specific method in the inner class.
Thinking of using:
1, hide the internal implementation.
2, with the template method for use.
Internal classes (Inner Class)