【问题标题】:Object selection and Printing from inside class类内的对象选择和打印
【发布时间】:2018-09-22 22:35:49
【问题描述】:

我试图让用户在运行时从我的 virtualZoo.java 文件中选择一种动物。它可以编译,但是一旦用户输入一个选择,我就会收到一个错误,上面写着“错误的树类型”。下面是 virtualZoo.java、animal.java 和 dog.java 的代码。我在 switch 语句下创建了对象,因为我被指示使用这些对象,但不了解实现。

virtualZoo.java

import java.util.Scanner;

public class VirtualZoo{
public static void main(String[] args) {

                    Animal cat = new Animal("Cat", "Meow");
                    Animal dog = new Animal("Dog", "Woof");
                    Animal duck = new Animal("Duck", "Quak");

    // create Scanner
    Scanner input;
    input = new Scanner(System.in);
        double userInput;
                    System.out.println("Welcome to the Zoo");
                    System.out.println("Pick select an animal to visit");
        System.out.println("=================================");
        System.out.println("===========MAIN MENU=============");
        System.out.println("=================================");
        System.out.println("==  0) Cat    ===================");
        System.out.println("==  1) Dog    ===================");
        System.out.println("==  2) Duck   ===================");
        System.out.println("== -1) EXIT   ===================");
        System.out.println("=================================");
        System.out.println();System.out.println();
        System.out.println( "Input  : ");
        Scanner sc = new Scanner(System.in);
        userInput = sc.nextInt();


    switch (sc.nextInt()) {
        case 0:
            System.out.println(cat);
            break;
        case 1:
            System.out.println(dog);
            break;
        case 2:
            System.out.println(duck);
            break;
        case -1:
            System.out.println("Your name is short length.");
            break;
        default:
            break;
    }
                    duck.speak();
                    dog.speak();
                    cat.speak();


  }

}

animal.java(virtualZoo.java 中的动物类)

public class Animal {
private String animalSound;
private String animalType = "";

//set animal sound
public void setSound(String sound) { this.animalSound = sound; }

//get animal sound
public String getSound() { return animalSound; }
public void setType(String type) { this.animalType = type; }

//get animal type
public String getType() { return animalType; }


public Animal(String animalType, String animalSound)
{
    this.animalSound = "";
    this.animalType = animalType;
    this.animalSound = animalSound;
}

public void speak()
 {
    System.out.println("The " + animalType + " says " + animalSound);
 }
}

dog.java(Animal 类中的 Dog 类)

public class Dog extends Animal {

public Dog(String animalType, String animalSound) {
    super(animalType, animalSound);
}

@Override
public void speak(){

    System.out.println("This dog barks");

  }
}

【问题讨论】:

  • 你是怎么得到这个错误的?发布的代码对我来说很好
  • 你学过枚举吗?如果是这样,我会将它们包括在我的答案中......
  • 如果是这种情况,我可能需要重新启动我的 IDE。每次我编译并输入用户输入时,我都会收到错误的树错误
  • 我还没学过枚举
  • 更新代码现在似乎正在编译,但我必须输入两次用户输入才能获得任何结果。代码仍然没有产生预期的结果。我应该使用不同的语句吗?可能是 If/Then 而不是 switch。

标签: java class inheritance extends


【解决方案1】:

你在正确的轨道上,但我相信你有一些错误。我假设您发布的代码与给您错误的代码略有不同(我测试过,但没有收到错误)

在这里,您从用户那里获得了 2 个输入。我相信你的意思是只得到 1 个。

userInput = sc.nextInt(); switch (sc.nextInt())

另外,userInput 应该在类型 int 上,而不是双精度。

你有一个狗类,但没有猫/鸭类

看起来您可能正在尝试使用工厂模式,但我不能 100% 确定。如果是这样,您可以创建一个 AnimalFactory 类。否则,“动物”类会令人困惑。我假设您没有使用工厂模式。

你会注意到一些事情:

我覆盖了 Object 的 toString() 方法。 System.out.println 将调用 toString() 方法。

Cat、Duck 和 Dog 都从 Animal 扩展而来,因此您的 Animal animalSelected 可以是这些类型中的任何一种!

Animal 有 2 个抽象方法。这些方法必须在任何子类(Cat、Dog、Duck)中被覆盖。声明方法意味着我们可以使用 Animal 类中的方法。它还允许我们调用任何动物的方法。

即使 Animal 没有定义该方法的主体,您也可以使用 animalSelected.getAnimalSound()

import java.util.Scanner;

public class VirtualZoo
{
    public static void main(String[] args) 
    {
    // Options
    final int catType = 0,
              dogType = 1,
              duckType = 2,
              exit = -1;

    // create Scanner
    Scanner input;
    input = new Scanner(System.in);
        int userInput;
                    System.out.println("Welcome to the Zoo");
                    System.out.println("Pick select an animal to visit");
        System.out.println("=================================");
        System.out.println("===========MAIN MENU=============");
        System.out.println("=================================");
        System.out.println("==  " + catType + ") Cat    ===================");
        System.out.println("==  " + dogType + ") Dog    ===================");
        System.out.println("==  " + duckType + ") Duck   ===================");
        System.out.println("== " + exit + ") EXIT   ===================");
        System.out.println("=================================");
        System.out.println();
        System.out.println();
        System.out.println( "Input  : ");
        Scanner sc = new Scanner(System.in);
        userInput = sc.nextInt();

    Animal animalSelected = null;

    switch (userInput) 
    {
        case catType:
            animalSelected = new Cat();
            break;
        case dogType:
            animalSelected = new Dog();
            break;
        case 2:
            animalSelected = new Duck();
            break;
        case -1:
            System.out.println("Your name is short length.");
            break;
        default:
            break;
    }

    if (animalSelected != null)
    {
        System.out.println(animalSelected);
    }
}

}

public abstract class Animal 
{
    public abstract String getAnimalSound();
    public abstract String getAnimalType();

    @Override
    public String toString()
    {
       return "The " + getAnimalType() + " says " + getAnimalSound();
    }
}

public class Duck extends Animal 
{
    @Override
    public String getAnimalSound() 
    {
        return "quacks";
    }

    @Override
    public String getAnimalType() 
    {
        return "Duck";
    }

}

public class Cat extends Animal 
{
    @Override
    public String getAnimalSound() 
    {
        return "meows";
    }

    @Override
    public String getAnimalType() 
    {
        return "Cat";
    }
}


public class Dog extends Animal 
{
    @Override
    public String getAnimalSound() 
    {
        return "barks";
    }

    @Override
    public String getAnimalType() 
    {
        return "Dog";
    }
}

【讨论】:

  • 再次感谢您。所有这些编辑都工作得很好。感谢您解释工厂模式。我相信这会在以后派上用场。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
相关资源
最近更新 更多