【发布时间】:2013-08-03 23:49:29
【问题描述】:
我一直在做一个关于类继承和抽象类的练习。我对这两个概念很有信心,但正如标题所示,我(仍然)无法将对象添加到类型类数组中。
问题如下:主要有3种文件,
- Zoo 类文件(包含 main 方法)
- 抽象类动物文件
- 以及任何数量的特定动物(牛、马等),它们是 你猜对了,类动物。
类动物园
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 条件。
对于我的生活,我无法理解,它不起作用。更糟糕的是,我在之前的练习中遇到了类似的问题:
为类 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;
}
}
派生类牛
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;
}
}
【问题讨论】:
-
你收到的是
NullPointer还是ArrayOutOfBounds? -
在
zoo()中定义animals=new Animal[#];
标签: java arrays class null nullpointerexception