不同于老師說(shuō)的結(jié)構(gòu),但實(shí)現(xiàn)同樣效果,不知道兩種方法有什么區(qū)別
每本書創(chuàng)建成單獨(dú)類調(diào)用
package com.xiaohongshu;
import java.util.Scanner;
public class Initail {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("書庫(kù)中的圖書目錄:");
System.out.println("1.《紅樓夢(mèng)》");
System.out.println("2.《三國(guó)演義》");
System.out.println("3.《水滸傳》");
? ? ? ? System.out.println("4.《西游記》");
? ? ? ??
? ? ? ? while(true) {
? ? ? ? System.out.println("請(qǐng)輸入圖書的編號(hào)或名字:");
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? String score=input.next();
? ? ? ? switch (score) {
case "1" :
case "紅樓夢(mèng)":
book hello=new HongLouMeng();
? ? ? ? hello.name();
? ? ? ? hello.category();
? ? ? ? hello.author();
? ? ? ? break;
case "2":
case "三國(guó)演義":
book hello2=new SanGuoYanYi();
? ? ? ? hello2.name();
? ? ? ? hello2.category();
? ? ? ? hello2.author();
? ? ? ? break;
case "3":
case "水滸傳":
book hello3=new ShuiHuZhuan();
? ? ? ? hello3.name();
? ? ? ? hello3.category();
? ? ? ? hello3.author();
? ? ? ? break;
case "4":
case "西游記":
book hello4=new SanGuoYanYi();
? ? ? ? hello4.name();
? ? ? ? hello4.category();
? ? ? ? hello4.author();
? ? ? ? break;
default:
System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入!");
continue;
}
? ? ? ?
//? ? ? ??
? ? ? ??
? ? ? ? }
}
}
2020-10-13
當(dāng)書本數(shù)量很多的時(shí)候,一個(gè)個(gè)創(chuàng)建類就麻煩了