从内部选择对象和打印 class
Object selection and Printing from inside class
我正在尝试让用户 select 在我的 virtualZoo.java 文件运行时使用动物。它可以编译,但是一旦用户输入 selection,我就会收到一个错误,显示为 "Erroneous tree type." 下面是 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内的动物Class)
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(动物class中的狗class)
public class Dog extends Animal {
public Dog(String animalType, String animalSound) {
super(animalType, animalSound);
}
@Override
public void speak(){
System.out.println("This dog barks");
}
}
你在正确的轨道上,但我相信你有一些错误。我假设您发布的代码与给您错误的代码略有不同(我测试过,但没有收到错误)
在这里,您从用户那里获得了 2 个输入。我相信你只想得到 1.
userInput = sc.nextInt();
switch (sc.nextInt())
此外,userInput
应该是 int
类型,而不是双精度。
你有一只狗class,但是没有cat/duckclass
看起来您可能正在尝试使用工厂模式,但我不是 100% 确定。如果是这样,您可以创建一个 AnimalFactory
class。否则 class 'Animal' 会令人困惑。我假设您没有使用工厂模式。
您会注意到一些事情:
我重写了 Object 的 toString()
方法。 System.out.println
将调用 toString()
方法。
Cat、Duck 和 Dog 都是从 Animal
扩展而来的,因此您的 Animal animalSelected
可以是这些类型中的任何一种!
Animal有2个抽象方法。这些方法必须在任何子 classes(Cat、Dog、Duck)中被覆盖。声明方法意味着我们可以在 Animal class 中使用方法。它还允许我们调用任何动物的方法。
E.G 你可以使用 animalSelected.getAnimalSound()
即使 Animal 没有定义那个方法的主体。
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";
}
}
我正在尝试让用户 select 在我的 virtualZoo.java 文件运行时使用动物。它可以编译,但是一旦用户输入 selection,我就会收到一个错误,显示为 "Erroneous tree type." 下面是 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内的动物Class)
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(动物class中的狗class)
public class Dog extends Animal {
public Dog(String animalType, String animalSound) {
super(animalType, animalSound);
}
@Override
public void speak(){
System.out.println("This dog barks");
}
}
你在正确的轨道上,但我相信你有一些错误。我假设您发布的代码与给您错误的代码略有不同(我测试过,但没有收到错误)
在这里,您从用户那里获得了 2 个输入。我相信你只想得到 1.
userInput = sc.nextInt();
switch (sc.nextInt())
此外,userInput
应该是 int
类型,而不是双精度。
你有一只狗class,但是没有cat/duckclass
看起来您可能正在尝试使用工厂模式,但我不是 100% 确定。如果是这样,您可以创建一个 AnimalFactory
class。否则 class 'Animal' 会令人困惑。我假设您没有使用工厂模式。
您会注意到一些事情:
我重写了 Object 的 toString()
方法。 System.out.println
将调用 toString()
方法。
Cat、Duck 和 Dog 都是从 Animal
扩展而来的,因此您的 Animal animalSelected
可以是这些类型中的任何一种!
Animal有2个抽象方法。这些方法必须在任何子 classes(Cat、Dog、Duck)中被覆盖。声明方法意味着我们可以在 Animal class 中使用方法。它还允许我们调用任何动物的方法。
E.G 你可以使用 animalSelected.getAnimalSound()
即使 Animal 没有定义那个方法的主体。
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";
}
}