public class Animal {
public Animal(String aType) {
type = new String(aType);
}
public String toString() {
return "This is a " + type;
}
private String type;
}
public class Dog extends Animal {
public Dog(String aName) {
super("Dog");
name = aName;
breed = "Unknown";
}
public Dog(String aName, String aBreed) {
super("Dog");
name = aName;
breed = aBreed;
}
private String name;
private String breed;
}
public class TestBerived {
public static void main(String[] args) {
Dog aDog = new Dog("Fido", "Chihuahua");
Dog starDog = new Dog("Lassie");
System.out.println(aDog);
System.out.println(starDog);
}
}
输出结果是:
This is a Dog
This is a Dog
[size=x-small]请问调用基类的构造函数为何会隐含调用基类的toString方法呢?[/size]