Programming | programs | inheriting | Examples about inheritance internal class--java programming thought sample program analysis;
Class Egg2 {
Protected class Yolk {
Public yolk () {
System.out.println ("Egg2.yolk ()");
}
public void F () {
System.out.println ("Egg2.yolk.f ()");
}
}
Private yolk y = new yolk ();
Public Egg2 () {
System.out.println ("New Egg2 ()");
}
public void insertyolk (yolk yy) {
y = yy;
}
public void G () {
Y.F ();
}
}
public class BigEgg2 extends Egg2 {
public class Yolk extends Egg2.yolk {
Public yolk () {
System.out.println ("Bigegg2.yolk ()");
}
public void F () {
System.out.println ("Bigegg2.yolk.f ()");
}
}
Public BigEgg2 () {
Insertyolk (new yolk ());
}
public static void Main (string[] args) {
EGG2 e2 = new BigEgg2 ();
E2.G ();
}
}
Program Run Result:
Egg2.yolk ()
New Egg2 ()
Egg2.yolk ()
Bigegg2.yolk ()
Bigegg2.yolk.f ()
Run Order:
1, Egg2 e2 = new BigEgg2 (); Subclasses call the parent class constructor before instantiating the parent class constructor to initialize the class member first;
There is a private yolk y = new yolk ();, y is initialized as a member of the EGG2 class first, the constructor of the Egg2 inner class yolk is called first, and the Egg2.yolk () is printed out;
2, call the Egg2 constructor, print out the Egg2.yolk ();
3, call the BigEgg2 constructor, because it insertyolk (new yolk ()); The new yolk here is an instance of the inner class of BigEgg2; This inner class inherits the inner class from the EGG2
, so we call the constructor of the yolk in Egg2 first, and print out egg2.yolk ();
4, call the Bigegg2.yolk constructor, print out the Bigegg2.yolk ();
5, E2.G (); Print out bigegg2.yolk ();
I do not know the analysis of the right, if not, please advise zhaoqb@neusoft.com
Or
Zqb@hanhuasoft.com
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.