我需要:使用 Lion 类扩展动物类并具有不同的功能(完成)。
添加一个名为 Liontype 类的字段,并添加一个根据重量对狮子类型进行分类的方法。(需要从超类派生)
并打印出来。
我的代码中有错误,我一直在尝试修复它。提前感谢您的帮助。
public class Animal {
private int numTeeth = 0;
private boolean spots = false;
private int weight = 0;
public Animal(int numTeeth, boolean spots, int weight){
this.setNumTeeth(numTeeth);
this.setSpots(spots);
this.setWeight(weight);
}
public int getNumTeeth(){
return numTeeth;
}
public void setNumTeeth(int numTeeth) {
this.numTeeth = numTeeth;
}
public boolean getSpots() {
return spots;
}
public void setSpots(boolean spots) {
this.spots = spots;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
//Extend animal class
public class Lion extends Animal{
public Lion (int numTeeth, boolean spots, int weight){
super(numTeeth, spots, weight);
//Add new attributes
int age = 0;
int cubs = 0;
}
public static void main(String args[]){
//Create lions and assign attributes
Lion lion1 = new Lion();
lion1.numTeeth = 12;
lion1.spots = 1;
lion1. weight = 86;
lion1.age = 7;
lion1.cubs = 3;
//Print attributes
System.out.println("Lion1 attributes:");
System.out.println("Number of teeth : " + numTeeth);
System.out.println("Number of spots : " + spots);
System.out.println("Weight of lion : " + weight + " kgs");
System.out.println("Age : " + age);
System.out.println("No of cubs : " + cubs);
System.out.println(" ");
Lion lion2 = new Lion();
lion2.numTeeth = 16;
lion2.spots = 0;
lion2. weight = 123;
lion2.age = 13;
lion2.cubs = 5;
System.out.println("Lion2 attributes:");
System.out.println("Number of teeth : " + numTeeth);
System.out.println("Number of spots : " + spots);
System.out.println("Weight of lion : " + weight + " kgs");
System.out.println("Age : " + age);
System.out.println("No of cubs : " + cubs);
System.out.println(" ");
}
哈士奇WWW
忽然笑
相关分类