我创建了动物园的模型。在我的动物园里,我只有鸟。在我的主要班级Zoopark,我把一只动物放在笼子里。目前,可以在笼子中添加无限数量的鸟类。但每个笼子里不应该有两只以上的鸟。我该如何进行此修订?
主要课程
public class Zoopark {
public static void main(String[] args) {
Bassein V1 = new Bassein();
Bird B = new Bird("Vala", 100, true);
V1.putAnimal(B);
Bird C = new Bird("Lilu", 100, true);
V1.putAnimal(C);
Bird D = new Bird("Abara", 100, true);
V1.putAnimal(D);
OpenCage V2 = new OpenCage();
Hippo E = new Hippo("Kala", 1000, true);
V2.putAnimal(E);
Hippo I = new Hippo("Nala", 1000, true);
V2.putAnimal(I);
Hippo F = new Hippo("Mala", 1000, true);
V2.putAnimal(F);
Hippo U = new Hippo("Ala", 1000, true);
V2.putAnimal(U);
}
}
带笼子的课程
import java.util.ArrayList;
import java.util.List;
public class Valers {
private final static int CageLimit=2;
List<Animals> result = new ArrayList<Animals>();
public void putAnimal(Animals a) {
int numAnimals=result.size();
if (numAnimals>=CageLimit) {
System.out.println("This cage is full!");
System.out.println(" ");
}
else {
this.result.add(a);
System.out.println("Animal added in cage");
System.out.println(" ");
}
}
public int getCageAnimals() {
return result.size();
}
}
Bassein笼子类:
public class Bassein extends Valers {
}
OpenCage笼子类:
public class OpenCage extends Valers {
}
抽象类动物:
abstract public class Animals {
private String name;
private int weight;
// is this boy?
private boolean isMale;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return weight;
}
public void setAge(int age) {
this.weight = weight;
}
public boolean isMale() {
return isMale;
}
public void setMale(boolean isMale) {
this.isMale = isMale;
}
public Animals(String name, int weight, boolean isMale) {
this.name = name;
this.weight = weight;
this.isMale = isMale;
}
}
班鸟:
public class Bird extends Animals {
public Bird(String name, int weight, boolean isMale) {
super(name, weight, isMale);
System.out.println(name + " " + weight + " " + isMale);
}
}
与河马一起上课:
public class Hippo extends Animals {
public Hippo(String name, int weight, boolean isMale) {
super(name, weight, isMale);
System.out.println(name + " " + weight + " " + isMale);
}
}
现在输出:
Vala 100 true
Animal added in cage
Lilu 100 true
Animal added in cage
Abara 100 true
This cage is full!
Kala 1000 true
Animal added in cage
Nala 1000 true
Animal added in cage
Mala 1000 true
This cage is full!
Ala 1000 true
This cage is full!
应该是:
Vala 100 true
Animal added in cage
Lilu 100 true
Animal added in cage
Abara 100 true
This cage is full
Kala 1000 true
Animal added in cage
Nala 1000 true
Animal added in cage
Mala 1000 true
Animal added in cage
Ala 1000 true
Animal added in cage
谢谢!
答案 0 :(得分:2)
在Valers.putAnimal()中添加动物之前添加它:
if (result.size() >= 2) {
System.out.println("This cage is full!");
return;
}
答案 1 :(得分:2)
虽然@ LW001的答案会给出正确的打印输出,但我相信,从逻辑上讲,你仍然会在笼子里加入第三只动物。可能更好的是你可以添加一种方法来检查笼子里的动物数量。然后添加一个if语句,仅在少于两只动物的情况下添加动物。例如:
import java.util.ArrayList;
import java.util.List;
public class Valers {
private final static int CageLimit=2;
List<Animals> result = new ArrayList<Animals>();
public void putAnimal(Animals a) {
int numAnimals=result.size();
if (numAnimals>=CageLimit) {
System.out.println("This cage is full!");
System.out.println(" ");
}
else {
this.result.add(a);
System.out.println("Animal added in cage");
System.out.println(" ");
}
}
public integer getCageAnimals() {
return result.size();
}
}
请注意,我为CageLimit添加了private final static int
。这是因为良好的实践要求不要在代码逻辑中包含“幻数”。因此,在开始时将其初始化为不可变值。