通过这个例子,我们可以比较清楚地看到父类和子类的静态变量,一般变量,构造函数的构造过程。以前我对这个过程也不是很清楚,但现在很清楚了。
public class OrderTest
{
public static void main(String[] args)
{
System.out.println("\nSubClass:");
new SubClass();
System.out.println("\nSub2Class");
new Sub2Class();
}
}
class ParentClass
{
String par_Stt = print("parent static");
String par_smp = print("parent simple");
ParentClass()
{
print("parent construct");
}
protected static String print(String str)
{
System.out.println(str);
return str;
}
}
class SubClass extends ParentClass
{
static String sub_stt = print("sub static");
String pre_smp = print("sub simple");
SubClass()
{
super();
print("sub construct");
}
}
class Sub2Class extends ParentClass
{
String sub2_smp = print("sub2 simple");
}
执行结果:
SubClass:
sub static
parent static
parent simple
parent construct
sub simple
sub construct
Sub2Class:
parent static
parent simple
parent construct
sub2 simple
阅读(1153) | 评论(0) | 转发(0) |