package testCasting;
public class Animal {
String eyes;
int age;
public Animal(){
}
}
class Dog extends Animal {
String furcolor;
public Dog(String eyes,int age,String furcolor){
super();
this.eyes=eyes;
this.age=age;
this.furcolor=furcolor;
}
public String toString(){
return this.eyes+this.age+this.furcolor;
}
}
class Cat extends Animal{
String weight;
public Cat(String eyes,int age,String weight){
super();
this.eyes=eyes;
this.age=age;
this.weight=weight;
}
public String toString(){
return this.eyes+this.age+this.weight;
}
}
网络视频说super()出现是因为要构建子类的一个对象,必须要先用super()构造一个父类的对象,而我们老师说,super()只是进行初始化的。那么super();在这里究竟起到了一个什么作用呢?
是不是如果子类里有构造方法或者在其他类里要调用这个子类的构造方法,就必须在这个构造方法里调用父类的构造方法?
追答对,调用是必须调用的,但是分显示调用和隐式调用
拿第二个例子来说
class Son {
Son(){print ('son')}; //输出father 和 son
}
class Son {
Son(){super();print ('son')}; //输出father 和 son
}
class Son {
Son(){super(36);print ('son')}; //输出father is 36 和 son
}
根据你调用的不同结果不同,但是一定会调用的