【问题标题】:Trouble adding object into constructor无法将对象添加到构造函数中
【发布时间】:2017-04-25 01:50:24
【问题描述】:

对于一个学校项目,我们应该创建一个简单的程序,该程序从用户那里获取一些关于狗的输入(名称、品种、年龄和体重)并将它们放入一个 ArrayList 中。一切似乎都在工作,但是为了测试程序,我想在我的方法 setUp() 中添加一些狗,这样您就可以测试功能而不必每次都添加新狗,这就是我迷路的地方!如果我在 setUp() 中编写构造函数 (Dog Dog = new Dog("Alex", "Pug", 10, 10.0),则会收到错误消息:

The value of the local variable Dog is not used.

我也尝试将构造函数放在“1”的情况下,然后我没有收到任何错误,但狗没有添加到数组列表中,而我可以在程序中添加新的狗。我真的不知道下一步该做什么。需要将狗添加到 Dog-constructor 中以进行分配(部分代码被裁剪为相关,不用担心导入或 main)。

class DogReg {
private ArrayList<Dog> allDogs = new ArrayList<Dog>();
private Scanner keyboard = new Scanner(System.in);

private void setUp() {
    System.out.print("Hi! Welcome to the Dog register! \n" + "Choose an option between 1-5\n");
    System.out.println("1. Register your dog");
    System.out.println("2. Increase age");
    System.out.println("3. List");
    System.out.println("4. Delete");
    System.out.println("5. Exit");
}

private void runCommandLoop() {

    // Initierar en while-loop som körs under tiden att willRun == true
    boolean willRun = true;
    while (willRun == true) {

        System.out.print("> ");

        // Skapar en variabel som konverterar input-sträng till lowerCase
        String command = keyboard.next();
        switch (command) {

        case "1":
            // Konstruerar en ny hund in i ArrayList
            Dog Dog = new Dog();
            // Sparar all input till ArrayList
            System.out.print("\nThe name of the dog: ");
            Dog.setName(keyboard.next());
            System.out.print("The breed of the dog: ");
            Dog.setBreed(keyboard.next());
            System.out.print("The age of the dog: ");
            int age = keyboard.nextInt();
            Dog.setAge(age);
            System.out.print("The weight of the dog: ");
            double weight = keyboard.nextDouble();
            Dog.setWeight(weight);
            allDogs.add(Dog);
            break;    

类狗{

private String name;
private String breed;
private int age;
private double weight;

public Dog (String name, String breed, int age, double weight){
    this.name = name;
    this.breed = breed;
    this.age = age;
    this.weight = weight;
}

public String getName() {
    return name;
}

public String getBreed() {
    return breed;
}

public int getAge() {
    return age;
}

public double getWeight() {
    return weight;
}

public void setName(String name) {
    this.name = name;
}

public void setBreed(String breed) {
    this.breed = breed;
}

public void setAge(int age) {
    this.age = age;
}

如果我使用此代码,我可以在 setUp() 中添加一只狗,但我们不应该使用它:

Dog Dog = new Dog();
Dog.setName("Bosse");
Dog.setBreed("Mops");
int age = 10;
Dog.setAge(age);
double weight = 10;
Dog.setWeight(weight);
allDogs.add(Dog);

希望它足够清楚,我为语法和/或拼写道歉,英语不是我的第一语言。

【问题讨论】:

  • 我没有仔细阅读你的代码,但有一件事:Dog Dog = new Dog(); - 不惜一切代价避免这样的命名,它最终会让你感到困惑并引入错误。因此,请尝试遵守 Java 命名约定,该约定规定变量名称应以小写字母开头,即Dog dog = new Dog();。 - 想想这一行发生了什么:Dog.setName(keyboard.next()); - 这里的Dog 是什么?您指的是类还是变量?编译器就是不知道,以后再读代码时你可能也不知道。
  • 你应该尊重java naming conventionDog dog = new Dog();
  • 如果您询问某些代码导致的错误,请发布准确完整的错误消息,并发布导致错误的代码。不是其他代码。

标签: java oop object arraylist constructor


【解决方案1】:

将您的 Dog 变量命名为不同的名称。您将变量命名为与对象名称相同的名称,这会使编译器感到困惑。试试这个,例如:

Dog newDog = new Dog();
newDog.setName("Bosse");
newDog.setBreed("Mops");
int age = 10;
newDog.setAge(age);
double weight = 10;
newDog.setWeight(weight);
allDogs.add(newDog);

【讨论】:

  • 感谢您的评论,确实有帮助!
  • @zkulltrail 确保您对您认为有帮助的答案进行投票,如果我的回答解决了您的问题,您可以单击复选标记接受它。欢迎来到本站!
  • 跟进问题。我想要一个对于我的案例“1”(我添加更多狗)为空的构造函数,但将其完全留空感觉很便宜,还有其他方法吗?编辑:我说的是:public DogArr (){ }
  • 使用空构造函数本身并没有错,但您可以考虑将用户的所有数据收集到变量中,然后将它们放入您的狗构造函数中。
猜你喜欢
  • 1970-01-01
  • 2014-04-22
  • 2013-10-27
  • 2014-09-18
  • 2021-05-16
  • 2019-04-22
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
相关资源
最近更新 更多