gpt4 book ai didi

java - 在Java中将对象添加到空数组中

转载 作者:行者123 更新时间:2023-11-30 04:17:22 24 4
gpt4 key购买 nike

我一直在做类继承和抽象类的练习。我对这两个概念非常有信心,但正如标题所示,我(仍然)在将对象添加到类型类数组时遇到困难。

问题如下:文件主要有3种类型,

  1. 类 Zoo 文件(包含 main 方法)
  2. 抽象类动物文件
  3. 以及任意数量的特定动物(牛、马等),它们是以下类别的派生类:你猜对了,动物类。

动物园类

public class Zoo 
{

private int actual_num_animals;
private int num_cages;
private Animal[] animals;


Zoo()
{
actual_num_animals = 0;
num_cages = 20;
}

Zoo(int num_cages)
{
this.num_cages = num_cages;
}

// adds an animal to Zoo
public void add(Animal a)
{
for(int i = 0; i < num_cages; i++)
{
if(animals[i] != null && animals[i].equals(a) == true)
{
System.out.println(a.getName() + " is already in a cage!");
break;
}
else if(animals[i] == null)
{
animals[i] = a;
actual_num_animals++;
break;
}
}

}



// returns the total weight of all animals in zoo
public double total_weight()
{
double sum = 0;

for(int i = 0; i < actual_num_animals; i++)
{
sum += animals[i].getWeight();
}
return sum;
}

//Print out the noises made by all of the animals.
//In otherwords, it calls the makeNoise() method
//for all animals in the zoo.
public void make_all_noises()
{
for(int i = 0; i < actual_num_animals; i++)
{
animals[i].makeNoise();
System.out.print("! ");
}
}

//prints the results of calling toString() on all animals in the zoo.
public void print_all_animals()
{
for(int i = 0; i < actual_num_animals; i++)
{
animals[i].toString();
System.out.print(" ");
}
}

public static void main(String[] args)
{
Zoo z = new Zoo();
Snake sly = new Snake("Sly", 5.0 , 2, 2);
Snake sly2 = new Snake("Slyme", 10.0 , 1, 2);
Cow blossy = new Cow("Blossy", 900., 5, 10);
Horse prince = new Horse("Prince", 1000., 5, 23.2);

// Following not allowed because Animal is abstract
//Animal spot = new Animal("Spot", 10., 4);

z.add(sly);
z.add(sly2);
z.add(blossy);
z.add(prince);

z.make_all_noises();
System.out.println("Total weight =" + z.total_weight());
System.out.println("**************************");
System.out.println("Animal Printout:");
z.print_all_animals();

}
}

我的问题出在此处的 add 方法中。我在第一个 if 语句处不断收到空指针异常

if(animals[i] != null && animals[i].equals(a) == true)

以及第一次在 main 方法中调用此 add 方法。清楚地,此条件以及伴随它的 else-if 条件可能有问题。

对于我的生活我无法理解,它不起作用。更糟糕的是,我在之前的练习中遇到了类似的问题:

Class Inheritance in Java

为 Zoo 类编写的 if 和 else-if 条件与上一个问题中概述的 add 函数中的格式完全相同,这是最令人困惑的一点。你们有什么想法吗?

最后,作为引用,尽管我怀疑您是否需要它,但我将在下面包含动物类文件和派生类牛文件:

抽象类动物

public abstract class Animal
{
private String name;
private double weight;
private int age;

Animal()
{
name = "noName";
weight = 0;
age = 0;
}

Animal(String n, double weight, int age)
{
name = n;
this.weight = weight;
this.age = age;
}

abstract String makeNoise();

String getName()
{
return name;
}

double getWeight()
{
return weight;
}

int getAge()
{
return age;
}

public String toString()
{
return name + ", weight: " + weight + "age: " + age;
}

}

派生类Cow

public class Cow extends Animal 
{
private int num_spots;

Cow()
{
super();
num_spots = 0;
}
Cow(String name, double weight, int age, int num_spots)
{
super(name, weight, age);
this.num_spots = num_spots;
}

String makeNoise()
{
return "Moooo";
}

public String toString()
{
return getName() + ", weight: " + getWeight() + "age: " + getAge() +
"num spots: " + num_spots;
}
}

最佳答案

向 Zoo 构造函数添加一行以初始化数组:

Zoo()
{
actual_num_animals = 0;
num_cages = 20;
animals = new Animal[num_cages];
}

您可能想要实现 equals() ("Bloch way" 是一个很好的实现)。

关于java - 在Java中将对象添加到空数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com