當子類繼承了父類之后,能不能重寫父類中的構(gòu)造方法?比如下面Dog類繼承了Animal類,但是父類的構(gòu)造方法中輸出的那句話怎樣在子類中重寫?
public class Animal {
int age;
float weight;
public static void eat(){
System.out.println("動物有吃東西的能力");
}
public Animal(){
System.out.println("Aniimal類執(zhí)行了");
}
public class Dog extends Animal{
public Dog(){
age=10;
weight=15.6f;
System.out.println("狗狗的年齡是:"+age);
System.out.println("狗狗的體重是:"+weight);
System.out.println("Dog類執(zhí)行了");
}
public static void eat(){
System.out.println("重寫父類方法后");
System.out.println("狗狗有吃東西的能力");
}
}
2016-03-25
父類的構(gòu)造方法,在創(chuàng)建對象的時候,首先調(diào)用父類的構(gòu)造方法,然后才開始子類,調(diào)用子類的構(gòu)造方法。所以重寫是不可能的。