public abstract class Animal {protected int legs;protected Animal(int legs){this.legs = legs;}public void walk(){System.out.printf("Animals walk with %d legs", this.legs);}}寫Cat類要求:1. 該類必須包含String屬性來存寵物的名字。2. 定義一個構(gòu)造器,它使用String參數(shù)指定貓的名字;該構(gòu)造器必須調(diào)用超類構(gòu)造器來指明所有的貓都是四條腿。3. 另定義一個無參的構(gòu)造器。該構(gòu)造器調(diào)用前一個構(gòu)造器(用this關(guān)鍵字)并傳遞一個空字符串作為參數(shù)
2 回答
慕標琳琳
TA貢獻1830條經(jīng)驗 獲得超9個贊
public class Cat extends Animal{ String name; public Cat(String name){ this.name=name; super(4); } public Cat(){ this(""); }}
心有法竹
TA貢獻1866條經(jīng)驗 獲得超5個贊
public class Cat extends Animals{
private String name;
protected Cat(String name,int legs) {
super(4);
this.name=name;
// TODO Auto-generated constructor stub
}
public Cat(){
this("",4);
walk() ;
}
@Override
public void walk() {
// TODO Auto-generated method stub
super.walk();
}
}
添加回答
舉報
0/150
提交
取消
