Java OOP继承作业(动物到狮子的超类继承)
我需要: 使用狮子类扩展动物类,并具有不同的特性 添加一个名为Liontype类的字段,并添加一个按权重对lion类型进行分类的方法。需要从超类派生 然后打印出来 我的代码中有错误,我一直在尝试修复它。 提前感谢您的帮助Java OOP继承作业(动物到狮子的超类继承),java,inheritance,Java,Inheritance,我需要: 使用狮子类扩展动物类,并具有不同的特性 添加一个名为Liontype类的字段,并添加一个按权重对lion类型进行分类的方法。需要从超类派生 然后打印出来 我的代码中有错误,我一直在尝试修复它。 提前感谢您的帮助 public class Animal { private int numTeeth = 0; private boolean spots = false; private int weight = 0; public Animal(int numTeeth,
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(" ");
}
public class Liontype{
//Trying to get weight from another class
public Integer getWeight()
{
if (weight > 120)
{
System.out.println("This lion is a cub");
}
else if (weight >= 120 && weight < 180)
{
System.out.println("This lion is a female");
}
else if (weight >= 180)
{
System.out.println("This lion is a male");
}
}
}
}
Expected outcome:
Lion attributes:
Number of teeth : 16
Spots : true
Weight of lion : 83kgs
Age : 13
No of cubs : 3
This lion is a female
是的,您的代码中存在许多问题,这些问题将在编译阶段获得。也许你没有正确地说明你的例子。因此,请提供您的问题的详细信息 我要指出一些显而易见的事实: 您声明了局部变量 int年龄=0; int-cubs=0 在构造函数中,它实际上没有使用新属性扩展Lion类。将这些属性作为字段添加到类Animal中: 然后根据需要在Lion类的构造函数中初始化它们 在方法public static void mainString args[]中,您试图使用 狮子类的田野年龄大,幼崽少。见第1点 Liontype类的公共整数getWeight有2个错误。首先,未定义可变权重,其次,尽管方法必须返回整数值,但缺少return语句
除了Dmitry指出的错误外,Lion类中的主要错误还包括:
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;
numTeeth spots weight和所有其他字段都设置为私有。您的Lion类无法直接访问这些字段。你应该用你的接球手和接球手来对付动物
在Lion中打印属性时,也要执行以下操作:
字段是对象的属性。尝试直接打印这些字段将导致编译器错误,因为这些是Lion1对象的属性。您需要像这样使用点运算符:
System.out.println("Number of Teeth" + Lion1.getNumTeeth());
什么错误?编译器还是运行时?请具体说明LionType resp在哪里。狮
//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(" ");
System.out.println("Number of Teeth" + Lion1.getNumTeeth());