我正在处理这个专为我练习继承而设计的训练营任务。基类如下所示:
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;
}
我应该创建一个名为 Lion 的子类,它继承自 Animal 类,具有以下特定要求:
为狮子类型(幼崽、雄性、雌性)添加一个字段。
在这个类中添加一个方法,根据它的权重设置狮子的类型(注意权重是从超类派生的字段)。
如果重量小于 80 公斤,它的类型应该是幼崽。如果小于 120kg,应该是女的。如果大于120kg,则为男性。
包括一个打印狮子对象描述的方法。
我已经尝试了几个小时来解决这个问题,但我能想到的最好的方法如下:
public class Lion extends Animal {
public Lion(int numTeeth, boolean spots, int weight) {
super(numTeeth, spots, weight);
}
void lionDisplay() {
String type = new String();
{
if (getWeight() >= 120) {
type.equals("Male");
}
else if (getWeight() < 120 && getWeight() >= 80) {
type.equals("Female");
}
else if (getWeight() < 80) {
type.equals("Cub");
}
System.out.println("Animal = Lion");
System.out.println("Teeth = " + getNumTeeth());
System.out.println("Spots = " + getSpots());
System.out.println("Weight = " + getWeight());
System.out.println("Type = " + type);
}
}
}
public class AnimalLauncher {
public void main(String[] args) {
Lion leo = new Lion(30, false, 90);
leo.lionDisplay();
}
}
}
我知道我做错了什么,但我不知道那是什么。由于某种原因,我根本无法显示“类型”。
这是我得到的输出:
Animal = Lion
Teeth = 30
Spots = false
Weight = 90
Type =
答案 0 :(得分:0)
您在这里似乎遇到的最大误解是 equals
方法,它实际上是一种用于比较两个实例并返回它们是否相等的方法。此方法不用于设置变量,唯一的方法是使用 =
运算符(或调用为您执行此操作的方法)。
因此,您最终要做的就是根据各种其他字符串检查 new String()
(通常应初始化为 ""
),这显然总是返回 false,然后继续。使用您的代码,您应该做的是这样的:
String type = "";
if (getWeight() >= 120) {
type = "Male";
}
else if (getWeight() < 120 && getWeight() >= 80) {
type = "Female";
}
else if (getWeight() < 80) {
type = "Cub"'
}
现在这将解决您的问题,但练习题实际上要求您为狮子类型添加一个字段,我假设它们是指类成员,因此是属于类状态的顶级变量。类似的东西:
public class Lion extends Animal {
String type = "Unknown";
}
然而,它也要求您根据动物的重量来设置它,这是在构造过程中通过超类 Animal
已知的。因此这个字段应该在构建阶段初始化,比如:
String type;
public Lion(int numTeeth, boolean spots, int weight) {
super(numTeeth, spots, weight);
if (weight >= 120) {
type = "Male";
}
else if (weight >= 80) {
type = "Female";
}
else {
type = "Cub";
}
}
答案 1 :(得分:-1)
我建议添加 type
作为类属性
public class Lion extends Animal {
private String type;
public Lion(int numTeeth, boolean spots, int weight) {
super(numTeeth, spots, weight);
this.type = type;
}
添加一个setter方法来改变值
public void setType(String type){
this.type = type;
}
在 lionDisplay
方法中
void lionDisplay() {
if (getWeight() >= 120) {
setType("Male");
}
else if (getWeight() < 120 && getWeight() >= 80) {
setType("Female");
}
else if (getWeight() < 80) {
setType("Cub");
}
System.out.println("Animal = Lion");
System.out.println("Teeth = " + getNumTeeth());
System.out.println("Spots = " + getSpots());
System.out.println("Weight = " + getWeight());
System.out.println("Type = " + type);
}
}
答案 2 :(得分:-1)
向 Lion
添加字段:
private String type;
为它添加一个 getter。
将String type
添加到Lion的构造函数中,它应该调用Animal的构造函数,然后赋值type
:
public Lion(int numTeeth, boolean spots, int weight, String type) {
super(numTeeth, spots, weight);
this.type = type;
}
那么:
System.out.println("Type = " + getType());
----
将所有字段设为 final
并删除所有 setter 方法 - Immutable object 是一种很好的做法。
另外,Animal
应该是一个 abstract
类。