クラス継承と抽象クラスの演習を行ってきました。私はこれら 2 つの概念にかなり自信を持っていますが、タイトルが示すように、型クラスの配列にオブジェクトを追加するのに (まだ) 問題があります。
問題は次のとおりです。ファイルには主に 3 つのタイプがあります。
- クラス Zoo ファイル (main メソッドを含む)
- 抽象クラスの動物ファイル
- そして、ご想像のとおり、animal クラスの派生クラスである特定の動物 (牛、馬など) の任意の数。
クラスズー
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 ステートメントで継続的に null ポインター例外が発生します
if(animals[i] != null && animals[i].equals(a) == true)
この add メソッドが main メソッド内で初めて呼び出されたときも同様です。明らかに、この条件には何か問題があり、それに付随する 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;
}
}