Java 不使用开关实现Setter和Getter
宠物可以是猫也可以是狗。每只宠物都需要有名字、主人的名字、颜色、医生的名字和品种。所有的宠物都会哭、吃、睡。 在没有使用switch方法的情况下,我尝试将setter和getter函数用于scanner。但是我不知道如何识别如果用户输入了狗,那么用户将输入关于狗的所有信息,然后输入猫 这可能吗Java 不使用开关实现Setter和Getter,java,switch-statement,java.util.scanner,getter-setter,Java,Switch Statement,Java.util.scanner,Getter Setter,宠物可以是猫也可以是狗。每只宠物都需要有名字、主人的名字、颜色、医生的名字和品种。所有的宠物都会哭、吃、睡。 在没有使用switch方法的情况下,我尝试将setter和getter函数用于scanner。但是我不知道如何识别如果用户输入了狗,那么用户将输入关于狗的所有信息,然后输入猫 这可能吗 package petexercise; import java.util.Scanner; public class PetCatDog { public static void main(St
package petexercise;
import java.util.Scanner;
public class PetCatDog {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
PetCat cat = new PetCat();
PetDog dog = new PetDog();
System.out.print("Enter Dog or Cat Word:");
String pet = cat.nextLine();
}
}
其他子类是为setter和getter创建的,其中提到的属性不是特定于动物的,因此可以有一个单独的宠物类,具有名称、颜色、品种和动物类型等属性:
enum Animal {
CAT(4),
DOG(4);
public final int legs;
private Animal(int legs) {
this.legs = legs;
}
}
public static Animal what(String whatAnimal) {
return Animal.valueOf(whatAnimal.toUpperCase());
};
Animal animal = what("dog");
我想如果你不想使用开关,你可以使用地图。第一步用简单的字符串识别用户输入的内容,第二步创建您的宠物
Map<String, PetFactory> factories = new HashMap<>();
factories.put("dog", new DogFactory())
...
String petType = scanner.nextLine();
factories.get(petType).createPet("name", "color");
Map factories=newhashmap();
factories.put(“dog”,新的DogFactory())
...
字符串petType=scanner.nextLine();
factories.get(petType).createPet(“名称”、“颜色”);
所以你有-
User—表示用户的类。有名字,宠物信息等。用户可以有很多或没有宠物
宠物-代表某种动物的宠物。有一些共同的属性,如姓名、年龄……一些共同的行为,如吃
狗-宠物有一些属性,比如名字,年龄,颜色,品种,一些动作特别适合狗吠叫,取球
猫-宠物有一些属性,比如名字、年龄、颜色、品种,还有一些行为,比如一天睡二十次没用
向用户询问-
Scanner scan = new Scanner(System.in);
String response;
do{
System.out.print("Do you have a pet ? (Y/N): ");
response = scan.nextLine();
} while(!response.equalsIgnoreCase("Y") && !response.equalsIgnoreCase("N"));
if(response.equalsIgnoreCase("N")){
System.exit(0);
}
do{
System.out.print("Cat or a dog ? (C/D): ");
response = scan.nextLine();
} while(!response.equalsIgnoreCase("C") && !response.equalsIgnoreCase("D"));
我明白了!谢谢大家的帮助 打包狗和猫 导入java.util.Scanner 公共类猫道系统{
public static void main(String[] args) {
String animal;
Scanner scan = new Scanner(System.in);
Cat cat = new Cat();
Dog dog = new Dog();
System.out.print("Enter Dog or Cat Only: ");
animal = scan.nextLine();
if(animal.equalsIgnoreCase("Cat")) {
System.out.print("Enter cat's name: ");
cat.setCatName(scan.nextLine());
System.out.print("Enter owner's name: ");
cat.setCatOwnersName(scan.nextLine());
System.out.print("Enter cat's color: ");
cat.setCatColor(scan.nextLine());
System.out.print("Enter doctor's name: ");
cat.setCatDoctorsName(scan.nextLine());
System.out.print("Enter cat's breed: ");
cat.setCatBreed(scan.nextLine());
System.out.println("");
System.out.println("Cat's Details");
System.out.println(cat.getCatName());
System.out.println(cat.getCatOwnersName());
System.out.println(cat.getCatColor());
System.out.println(cat.getCatDoctorsName());
System.out.println(cat.getCatBreed());
} else if(animal.equalsIgnoreCase("Dog")) {
System.out.print("Enter Dog's name: ");
dog.setDogName(scan.nextLine());
System.out.print("Enter owner's name: ");
dog.setDogOwnersName(scan.nextLine());
System.out.print("Enter Dog's color: ");
dog.setDogColor(scan.nextLine());
System.out.print("Enter doctor's name: ");
dog.setDogDoctorsName(scan.nextLine());
System.out.print("Enter Dog's breed: ");
dog.setDogBreed(scan.nextLine());
System.out.println("");
System.out.println("Dog's Details");
System.out.println(dog.getDogName());
System.out.println(dog.getDogOwnersName());
System.out.println(dog.getDogColor());
System.out.println(dog.getDogDoctorsName());
System.out.println(dog.getDogBreed());
} else System.out.println("Invalid Input !");
}
}你能发布你的代码吗?请出示一些代码!如果没有代码,则无法回答您是否使用switch欢迎使用SO。请发布到目前为止你已经想出的代码,否则很难给你有用的提示。为什么你不想在这种情况下使用switch虽然我喜欢枚举,但我认为它们在这里是一个糟糕的选择:枚举本身就是单例,而可能有很多狗的实例。我唯一的问题是每只宠物都会哭,吃和睡。如果宠物哭着打印“呼呼”,如果宠物吃了打印“宠物食品”,如果宠物睡觉打印“zzzz”…而不是使用do while。我曾经用过,如果不是的话if@SernielBarbosa如果不使用循环,则如果用户键入cst而不是cat,则不会给用户第二次机会:)我用了其他的。所以它可能会带着警告回来