gpt4 book ai didi

java - 无法创建构建动物园的容器

转载 作者:行者123 更新时间:2023-12-02 02:34:43 25 4
gpt4 key购买 nike

我正在尝试实现一个 Zoo 类,它将保存您的动物( Pandas 和大象)。在此类中,您应该有一个 Animal 对象的列表或数组,并且该容器应该能够容纳至少 5 只动物

您的 Zoo 类还应该有一个函数 void addAnimal(Animal a) ,允许您将动物添加到动物园容器中。这些 Animal 对象应该是您的子类的实例,并且此函数不应返回任何内容。

现在您已经拥有了所有对象,是时候编写 main() 函数来测试它们了。创建一个 ZooBuilder 类,其中包含一个 main() 函数,该函数创建一个 Zoo 和五个 Animals,然后添加动物动物园。这一切都很好,但还没有给您任何有用的输出。

因此,扩展您的 Animal 类,使其具有一个 void printInfo() 方法来打印 Animal 的名称、物种和年龄在同一条线上。因此,举例来说,如果您有一只名为“Spot”的 Panda,已有 10 岁了,则 printInfo() 函数应打印类似于以下内容的输出:

我为 Animal 创建了类,该类扩展到 PandaElephant。我(尝试)创建将添加动物的动物园容器。

当我运行 Zoobuilder 类和 main 函数时,返回是:

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: Zoo cannot be resolved to a variable"

import java.util.ArrayList;

public class Animal {
String name;
String species;
int age;
public Animal () {}
public Animal (String name, String species, int age){
this.name = name;
this.species = species;
this.age = age;
}
}

public class Elephant extends Animal {

public String species;

public Elephant () {

}
public Elephant (String name, String species, int age){
super (name, species, age);
this.name = "Elle";
this.species = "Elephant";
}
}

public class Panda extends Animal {
public String species;

public Panda () {}

public Panda (String name, String species, int age){

super (name, species, age);
this.name = "Spot";
this.species="Panda";
}
}

public class Zoo extends Animal {

public ArrayList<Animal> animals = new ArrayList<Animal>();

public void addAnimal(Animal a)
{
animals.add(a);
}

void printAllInfo()

public class Zoobuilder {

public static void main(String[] args) {
// TODO Auto-generated method stub
Zoo = new Zoo();

Panda Spot = new Panda ("Spot", "Panda", 0);
Elephant Elle = new Elephant ("Elle", "Elephant", 0);
new Animal ();
new Animal ();
new Animal ();
}

最佳答案

您的代码看起来很接近。这里有一些建议。

  1. 修改 Zoo 类,使其不扩展 Animal。动物园可以包含动物,但动物园本身的定义并不是某种动物。因此,将其更改为:class Zoo extends Animal 为:class Zoo。这是一项改进,您可以选择保持原样,但您的“动物园”似乎不太可能也是“动物”。
  2. 声明一个适当的“zoo”变量。现在,您遇到了编译错误,因此请将 Zoo = new Zoo(); 更改为:Zoo theZoo = new Zoo();。然后您将有一个名为“theZoo”的变量供稍后使用。
  3. 遵循命名约定并以小写字母开头变量名称。使用小写名称,而不是声明变量“Spot”(大写“S”)和“Elle”(大写“E”)的 Panda SpotElephant Elle : Pandas 斑大象elle
  4. 最后,将动物添加到您的动物园:theZoo.addAnimal(spot);theZoo.addAnimal(elle);
  5. 删除species的重复变量。每个 Animal 都有:Stringspecies; – 它定义了一个具有包范围的species成员变量。 ElephantPanda 分别定义了以下内容:public Stringspecies – 使用 public 定义了不同的 species 成员变量范围。关于这一点当然还有更多要说的,但这里是修复代码的建议:在 Animal 上保留一个 species 变量,并删除其他变量。

作为最后一点的示例,这里是定义一个新的 Cat 类所需的全部内容(请注意 Cat 上没有定义任何成员变量 - 它们不是'不需要,因为它从 Animal 获取它们)

class Cat extends Animal {
public Cat(String name, String species, int age) {
super(name, species, age);
}
}

这里有一些创建猫并打印它的名字的代码:

Cat cat = new Cat("Foofy", "Tabby", 7);
System.out.println(cat.name);

关于java - 无法创建构建动物园的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57211541/

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