2.只有普通方法的调用可以是多态的,域、静态方法都不算,当子类对象转型为父类引用时,任何域的访问操作都将由编译器解析,因此不是多态的。所以只会得到父类的域值,即使是public
当我们new出子类对象的时候,在这一过程中,应该先调用最最基类的构造函数。
1.调用基类的构造器。这个步骤会不断地反复递归下去,首先是构造这种层次结构的根,然后是下一层导出类,直到最低层的导出类。
2.按声明顺序调用成员的初始化方法
3.调用导出类构造器的主体
原因:在构造器内部,我们必须确保所要使用的成员都已经构建完毕。所以唯一的办法就是,首先调用基类构造器,那么在进入导出类构造器时,在基类中可供我们访问的成员都已经得到初始化了。
class Characteristic{
private String s;
Characteristic(String s){
this.s = s;
print("Creating Characteristic " + s);
}
protected void dispose(){
print("disposing Charactisric " + s);
}
}
class Description{
private String s;
Description(String s){
this.s = s;
print("Creating Description" + s);
}
protected void dispose(){
print("disposing Description" + s);
}
}
class LivingCreature{
private Characteristic p = new Characteristic("is alive");
private Description t = new Description("Basic Living Creature");
LivingCreature(){
print("LivingCreature() ");
}
protected void dispose(){
print("LivingCreature dispose ");
t.dispose();
p.dispose();
}
}
class Animal extends LivingCreature{
private Characteristic p = new Characteristic("has heart");
private Description t = new Description("Animal not Vegetable");
Animal (){
print("Animal ()");
}
protected void dispose(){
print("Animal dispose ");
t.dispose();
p.dispose();
super.dispose();
}
}
class Amphibian extends Animal {
private Characteristic p = new Characteristic("can live in water ");
private Description t = new Description("Both water and land ");
Amphibian (){
print("Amphibian l ()");
}
protected void dispose(){
print("Amphibian dispose ");
t.dispose();
p.dispose();
super.dispose();
}
}
public class Frog extends Amphibian{
private Characteristic p = new Characteristic( "Croaks");
private Description t = new Description( "Eats Bugs");
public Frog(){
print("Frog()");
}
protected void dispose(){
print("Frog dispose");
t.dispose();
p.dispose();
super.dispose();
}
public static void main(String[] args){
Frog frog = new Frog();
print("Bye!");
frog.dispose();
}
}
输出如下:
Creating Characteristic is alive
Creating Description Basic Living Creature
LivingCreature()
Creating Characteristic has heart
Creating Description Animal not Vegetable
Animal()
Creating Characteristic can live in water
Creating Description Both water and land
Amphibian()
Creating Characteristic Croaks
Creating Description Eats Bugs
Frog()
Bye!
Frog dispose
disposing Description Eats Bugs
disposing Characteristic Croaks
Amphibian dispose
disposing Description Both water and land
disposing Characteristic can live in water
Animal dispose
disposing Description Animal not Vegetable
disposing Characteristic has heart
LivingCreature dispose
disposing Description Basic Living Creature
disposing Characteristic is alive
1.先执行main(其实是先执行静态方法的,在这个类中,只有main函数是静态的)
2.组合对象在构造器之前执行
3.销毁与继承顺序相反
构造:先执行父类的构造函数 LivingCreature--->Animal--->Amphibian--->Frog
销毁:先销毁子类的 Frog--->Amphibian--->Animal--->LivingCreature
注意点:
1.如果成员对象中存在于其他一个或多个对象共享的情况,就不能简单的dispose了,而是应该计数来跟踪访问共享对象的对象数量;
2.程序中打印对象的话,其实是调用对象的toString()方法。