簡體   English   中英

不使用開關實現Setter和Getter

[英]Implementation of Setter and Getter without using switch

寵物可以是貓還是狗。 每只寵物都需要有一個名字,一個主人的名字,顏色,醫生的名字和品種。 所有的寵物都可以哭泣,吃飯和睡覺。 不使用切換方法,我嘗試使用掃描儀的setter和getter功能。 但我不知道如何識別用戶是否輸入狗然后用戶將輸入所有關於狗,然后貓輸入。

這可能嗎?

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創建的

我想如果你不想使用開關你可以使用地圖。 第一步用簡單的字符串識別用戶輸入的內容,第二步創建您的寵物。

Map<String, PetFactory> factories = new HashMap<>();
factories.put("dog", new DogFactory())
...
String petType = scanner.nextLine();
factories.get(petType).createPet("name", "color");

所以你有了 -

用戶 - 表示用戶的類。 有姓名,寵物信息等。用戶可以有很多或沒有寵物

寵物 - 代表某種動物的寵物。 有一些共同的屬性,如姓名,年齡..一些常見的行為,如吃。

狗 - 延伸寵物有一些屬性,如名稱,年齡,顏色,品種,一些特定的狗狗行動,取球

貓 - 延伸寵物有一些屬性,如姓名,年齡,顏色,品種,一些行為,如每天睡20次無用

詢問用戶 -

  1. 用戶信息(構建用戶)
  2. 詢問用戶是否有寵物。 如果沒有寵物,請更新用戶個人資料以聲明沒有寵物。
  3. 如果有寵物,請詢問他們是否有狗。 如果他們有,請問多少。 閱讀每只狗的信息。 構建用戶的狗配置文件。 如果沒有狗,請更新用戶個人資料以表明沒有狗。
  4. 如果有寵物,請詢問他們是否有貓。 如果他們有,請問多少。 閱讀每只貓的信息。 建立用戶的貓配置文件就像你在第3步中為狗做的那樣..

更新:

如果您知道用戶是輸入了狗還是貓,您可以使用以下內容:

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")); 

提到的屬性不是動物特定的,因此可以有一個Pet類,其中包含名稱,顏色,品種和動物類型等屬性:

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");

我知道了 ! 感謝大家的幫助

包dogandcat;

import java.util.Scanner;

公共類CatDogSystem {

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 !");
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM