Java 在类中添加实例变量的方法?
嗨,我正在尝试添加每个动物亚类(老虎、长颈鹿、企鹅)的所有空间要求,以便在动物园类中打印出来。根据我目前的计划(如下),我得到的每个尺寸要求都是0。加上一些额外的随机零。帮助??请,谢谢你Java 在类中添加实例变量的方法?,java,Java,嗨,我正在尝试添加每个动物亚类(老虎、长颈鹿、企鹅)的所有空间要求,以便在动物园类中打印出来。根据我目前的计划(如下),我得到的每个尺寸要求都是0。加上一些额外的随机零。帮助??请,谢谢你 public class Zoo { private Animal animal; public int number_animals;//# animals allowed in zoo private List<Animal> animal_list;
public class Zoo {
private Animal animal;
public int number_animals;//# animals allowed in zoo
private List<Animal> animal_list;
private String zoo_name;
public Zoo(String name){
this.zoo_name = name;
animal_list = new ArrayList<Animal>();
}
public void addAnimal(Animal obj) {
animal_list.add(obj);
}
public String toString(){
String s = "In zoo " + zoo_name + " there are the following animals:\n";
for (int i = 0; i < animal_list.size(); i++){
s += animal_list.get(i).printDetails() + "\n";
}
return s;
}
public String countAnimals(){
return "There are " + animal_list.size() + " animals in " + zoo_name + ".";
}
public String spaceRequirement(){
int s = 0;
int t = 0;
int k = 0;
for (int i = 0; i < animal_list.size(); i++){
if (animal_list.get(i) instanceof Giraffe){
while (! (animal_list.get(i) instanceof Giraffe))
s += animal_list.get(i).getSpaceRequired();
}
System.out.println(s);
if (animal_list.get(i) instanceof Penguin){
while (! (animal_list.get(i) instanceof Penguin))
t += animal_list.get(i).getSpaceRequired();
}
if (animal_list.get(i) instanceof Tiger){
while (! (animal_list.get(i) instanceof Tiger))
k += animal_list.get(i).getSpaceRequired();
}
}
return "Total tree height needed: " + t + "\nTotal water volume needed: " + s + "\nTotal grass area needed: " + k;
}
public void printDetails(){
System.out.println("In zoo " + zoo_name + " there are the following animals:\n" + animal);
}
public class Animal {
public String name;
public int weight;
private String food_type;
private int space_area = 0;
private Zoo zoo;
private Animal animal;
public Animal(String name, int weight){
this.name = name;
this.weight = weight;
}
public String getName(){
return this.name;
}
public int getWeight(){
return this.weight;
}
public int getSpaceRequired() {
return this.space_area;
}
public void printDailyFoodOrder() {
return;
}
public void setZoo(Zoo zoo){
this.zoo = zoo;
this.zoo.addAnimal(this);
}
public String printDetails() {
String a = getName() + " who weighs " + getWeight() + " kg,";
return a;
}
}
public class Giraffe extends Animal {
private String food_type = "leaves";
private int leaves;
private int space_area;
private int number_giraffes = 0;
public Giraffe[] giraffe_array;
public Giraffe(String name, int weight, String food_type, int space_area, int leaves){
super(name, weight);
this.food_type = food_type;
this.space_area = space_area;
this.leaves = leaves;
}
public int getNumberGiraffes(){
return this.number_giraffes;
}
public String printDetails(){
return super.printDetails() + " eats " + this.food_type + ", and needs a tree with a mininum height " + this.space_area + " meters.";
}
public int getSpaceRequired() {
return this.space_area;
}
}
public class Penguin extends Animal {
private String food_type = "fish";
private int food;
private int space_area;
public Penguin(String name, int weight, int food, int space_area){
super(name, weight);
this.food = food;
this.space_area = space_area;
}
public int getSpaceRequired() {
return this.space_area;
}
public void calcPoolSize() {
}
public String printDetails(){
return super.printDetails() + " eats " + this.food_type + ", and needs a pool sized " + this.space_area + " cubic meters.";
}
}
public class Test {
public static void main(String[] args) {
Zoo diego = new Zoo("San Diego");
Animal giffy = new Animal("Giffy", 950);
giffy = new Giraffe("Giffy", 950, "Leaves", 15, 100);
Animal gunther = new Animal("Gunther", 950);
gunther = new Giraffe("Gunther", 950, "Leaves", 15, 100);
Animal kevin = new Animal("Kevin", 225);
kevin = new Tiger("Kevin", 225, 10, 10);
Animal hobbes = new Animal("Hobbes", 200);
hobbes = new Tiger("Hobbes", 200, 8, 20);
Animal wobbles = new Animal("Wobbles", 25);
wobbles = new Penguin("Wobbles", 25, 5, 125);
Animal gretchin = new Animal("Gretchin", 15);
gretchin = new Penguin("Gretchin", 15, 3, 125);
diego.addAnimal(giffy);
diego.addAnimal(gunther);
diego.addAnimal(wobbles);
diego.addAnimal(hobbes);
diego.addAnimal(kevin);
diego.addAnimal(gretchin);
System.out.println(diego);
System.out.println(diego.spaceRequirement());
}
}
公共类动物园{
私人动物;
公共整数动物;允许动物进入动物园
私人名单动物名单;
私有字符串名称;
公共动物园(字符串名称){
this.zoo_name=名称;
animal_list=新数组列表();
}
公共动物(动物对象){
动物列表。添加(obj);
}
公共字符串toString(){
字符串s=“In zoo”+zoo\u name+“有以下动物:\n”;
对于(int i=0;i
您不需要这些while循环
public String spaceRequirement(){
int s = 0;
int t = 0;
int k = 0;
for (int i = 0; i < animal_list.size(); i++){
if (animal_list.get(i) instanceof Giraffe){
s += animal_list.get(i).getSpaceRequired();
}
System.out.println(s);
if (animal_list.get(i) instanceof Penguin){
t += animal_list.get(i).getSpaceRequired();
}
if (animal_list.get(i) instanceof Tiger){
k += animal_list.get(i).getSpaceRequired();
}
}
return "Total tree height needed: " + t + "\nTotal water volume needed: " + s + "\nTotal grass area needed: " + k;
}
公共字符串分隔符要求(){
int s=0;
int t=0;
int k=0;
对于(int i=0;i