因为要先初始化基类,然后再初始化子类;不能反过来,因为先有父类,再有子类,子类是从父类继承来的。

package dong;

public class A {

public static void main(String[] args) {
// TODO 自动生成的方法存根
Children children=new Children();
children.Myname();
}

package dong;

public class Parents {
void Myname()
{
System.out.println("Parents");
}
}

package dong;

public class Children extends Parents{
void Myname()
{
super.Myname();
System.out.println("Children");
}
}

 

}

运行结果截图:

预测:

Child.printValue().myValue200
Child.printValue().myValue200
Child.printValue().myValue201

程序运行结果:

Child.printValue().myValue200
Child.printValue().myValue200
Child.printValue().myValue201

第一行输出的结果是因为在程序中把child赋值给parent,因此parent的myValue变成了child中的值。第二行输出的结果是因为进行了++运算后,myValue的值还未发生变化,因此还输出200,而第三行输出变化后的值。

在java中,子类可以直接给父类赋值,反过来则要进行强制类型转换。