为什么创建了子类对象,父类构造函数就不能用了?
public class Animal {
public int age=10;
public String color;
public void eat() {
System.out.println("动物会吃东西!");
}
public Animal() {
System.out.println("Animal类执行了!");
age=20;
}
}
public class Dog extends Animal {
public Dog() {
super();
age=10;
System.out.println("Dog被执行了!");
}
public void method() {
System.out.println(super.age);
}
}
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
Dog dog=new Dog();
dog.method();
}
}
输出结果为
Dog被执行了!
10
为什么没有 Animal类执行了!