子类对父类的方法重写满足哪些规则?
package today1108;
class Creature{
public Creature(){
//隐示调用:构造器Object()
System.out.println("没有参数的顶级构造器");
}
}
class Animal extends Creature{
public Animal(String name){
//隐示调用:Creature();
System.out.println("Animal带一个参数的构造器"+name);
}
public Animal(String name,int age){
//隐示调用:Creature();
this(name); //调用本类构造器Animal(String name);但同本类构造器已经调用过Creature()所以无需再重复调用
System.out.println("Animal带两个参数的构造器"+age);
}
}
public class Wolf extends Animal{
public Wolf(){
//隐示调用顶级父类:Creature();
super("大灰狼",3);//隐示再调用父类构造器Animal(),由于父类构造器没有Animal(),会报错,所以必须用super来指定父构造器
System.out.println("Wolf没有参数构造器");
}
public static void main(String[] args){
new Wolf();
//new Animal("hhf");
}
}