当前位置: 代码迷 >> Eclipse >> 哪位解释一下这个代码的调用顺序解决思路
  详细解决方案

哪位解释一下这个代码的调用顺序解决思路

热度:20   发布时间:2016-04-23 14:11:30.0
哪位解释一下这个代码的调用顺序
Java code
class Animal{//7    Animal(){        System.out.println("Class Animal");    }}class Fish extends Animal{//8    Fish(){        System.out.println("Class Fish");    }}class GoldFish extends Fish{//9    GoldFish(){        System.out.println("Class GoldFish");    }}public class TestInheritanceConstructor extends Animal{//1    private GoldFish goldfish = new GoldFish();//2        public TestInheritanceConstructor(){ //3        System.out.println("TestInheritanceConstructor");//4    }        public static void main(String[] args){//5        new TestInheritanceConstructor();//6    }}
//这个代码从 哪里开始的。。之后 怎么调用的,,,哪位给 解释一下,,可以直接说代码后面的 数字就行。。。谢谢,,,

------解决方案--------------------
可以把你的代码改一下会看的更加清楚如:
public class testConstructor extends Animal {
private GoldFish goldfish = new GoldFish("1");// 2

public testConstructor() { // 3
super("2");
System.out.println("TestInheritanceConstructor");// 4
}

public static void main(String[] args) {// 5
new testConstructor();// 6
// GoldFish goldfish = new GoldFish();// 2
}

}

class Animal {// 7
Animal(String name) {
System.out.println("Class Animal:" + name);
}
}

class Fish extends Animal {// 8
Fish(String name) {
super(name);
System.out.println("Class Fish:" + name);
}
}

class GoldFish extends Fish {// 9
GoldFish(String name) {
super(name);
System.out.println("Class GoldFish:" + name);
}
}
结果是:
Class Animal:2
Class Animal:1
Class Fish:1
Class GoldFish:1
TestInheritanceConstructor