整理了一下,把多個(gè)對(duì)象保存起來,這樣后期如果需要統(tǒng)計(jì)載人量或者載貨量相關(guān)的也可以。
package?com.car; public?class?Car?{ ????protected?String?carName;???//汽車名稱 ????protected?Double?rent;??????//租金 ????public?Car(String?carName,?double?rent){ ????????this.carName?=?carName; ????????this.rent?=?rent; ????} ????public?String?getCarName(){ ????????return?this.carName; ????} ????public?Double?getRent(){ ????????return?this.rent; ????} }
AManned
package?com.car; /** ?*?載人汽車 ?*/ public?class?AManned?extends?Car{ ????protected?int?personCount; ????public?AManned(String?carName,?double?rent,?int?personCount){ ????????super(carName,?rent); ????????this.personCount?=?personCount; ????} ????public?int?getPersonCount(){ ????????return?this.personCount; ????} }
ATruck:
package?com.car; /** ?*?貨車 ?*/ public?class?ATruck?extends?Car{ ????protected?int?productCount; ????public?ATruck(String?carName,?double?rent,?int?productCount){ ????????super(carName,?rent); ????????this.productCount?=?productCount; ????} ????public?int?getProductCount(){ ????????return?this.productCount; ????} }
AMulti:
package?com.car; /** ?*?多功能汽車 ?*/ public?class?AMulti?extends?Car{ ????protected?int?productCount; ????protected?int?personCount; ????public?AMulti(String?carName,?double?rent,?int?personCount,??int?productCount){ ????????super(carName,?rent); ????????this.productCount?=?productCount; ????????this.personCount?=?personCount; ????} ????public?int?getPersonCount(){ ????????return?this.personCount; ????} ????public?int?getProductCount(){ ????????return?this.productCount; ????} }
index:
package?com.car; import?java.util.Scanner; public?class?index?{ ????public?static?void?main(String[]?args){ ????????//引導(dǎo)語 ????????System.out.println("歡迎使用租車系統(tǒng)"); ????????System.out.println("您是否要租車?1-是,0-否"); ????????Scanner?input?=?new?Scanner(System.in); ????????int?isRent?=?input.nextInt();???????//是否租車 ????????if?(isRent?==?1)?{ ????????????System.out.println("您可租車的類型和價(jià)目表:"); ????????????System.out.println("序號(hào)?汽車名稱?租金?容量"); ????????????//聲明所有汽車并顯示 ????????????Car[]?cars?=?{ ????????????????????new?AManned("奧迪",500,?4), ????????????????????new?AManned("馬自達(dá)",400,?4), ????????????????????new?AMulti("皮卡雪",450,?4,?2), ????????????????????new?AManned("金龍",800,?20), ????????????????????new?ATruck("松花江",?400,?4), ????????????????????new?ATruck("依維柯",1000,?20) ????????????}; ????????????for?(int?i?=?0;?i?<?cars.length;?i++)?{ ????????????????int?index?=?i+1; ????????????????if?(cars[i]?instanceof?AManned)?{ ????????????????????//載人 ????????????????????AManned?car?=?(AManned)?cars[i]; ????????????????????System.out.println( ????????????????????????????index?+?"?"+ ????????????????????????????car.getCarName()+?"?"+ ????????????????????????????car.getRent()+?"元/天?"+ ????????????????????????????"載人:"+?car.getPersonCount()+?"人?" ????????????????????); ????????????????}?else?if(cars[i]?instanceof?ATruck)?{ ????????????????????//載貨 ????????????????????ATruck?car?=?(ATruck)?cars[i]; ????????????????????System.out.println( ????????????????????????????index?+"?"+ ????????????????????????????car.getCarName()+?"?"?+ ????????????????????????????car.getRent()+?"元/天?"?+ ????????????????????????????"載貨:"+?car.getProductCount()+"噸" ????????????????????); ????????????????}?else?{ ????????????????????AMulti?car?=?(AMulti)?cars[i]; ????????????????????System.out.println( ????????????????????????????index+?"?"+ ????????????????????????????car.getCarName()+?"?"?+ ????????????????????????????car.getRent()+?"元/天?"?+ ????????????????????????????"載人:"+car.getPersonCount()+?"人?"+ ????????????????????????????"載貨:"+car.getProductCount()+"噸"); ????????????????} ????????????} ????????????//選擇要租的車,并記錄下來 ????????????System.out.println("請(qǐng)選擇要租車的數(shù)量:"); ????????????int?carsCount?=?input.nextInt(); ????????????Car[]?chooseCar?=?new?Car[carsCount]; ????????????for?(int?j?=?0;?j?<?carsCount;?j++)?{ ????????????????int?cycle?=?j+1; ????????????????System.out.println("請(qǐng)選擇第"+?cycle?+"輛車:"); ????????????????int?carNum?=?input.nextInt(); ????????????????chooseCar[j]?=?cars[carNum-1]; ????????????} ????????????//選擇租車天數(shù),并計(jì)算出租金 ????????????System.out.println("請(qǐng)輸入要租的天數(shù):"); ????????????int?days?=?input.nextInt(); ????????????double?singleSum?=?0; ????????????double?sum?=?0; ????????????for?(Car?aCar:?chooseCar)?{ ????????????????singleSum?+=?aCar.getRent(); ????????????} ????????????sum?=?singleSum?*?days; ????????????System.out.println("您的租金是:"+sum); ????????} ????} }
2020-02-23
for
?(Car?aCar:?chooseCar)
這里的Car aCar是什么,這個(gè)循環(huán)怎么看,這知識(shí)點(diǎn)不會(huì)啊,“:”是什么意思啊,大神求解答
2020-02-15
很強(qiáng),看完之后深受啟發(fā),感謝
2020-02-15
樓主,我想問一下,你那個(gè)強(qiáng)制引用類型轉(zhuǎn)換的意義在哪?我在想不用強(qiáng)制類型轉(zhuǎn)換,用cars[i].方法這樣形式去操作也行啊,麻煩解答一下謝謝。
2020-02-13
厲害?!
2020-02-12
厲害?!
2020-02-12
感覺這一季的大多知識(shí)都用上了,就是沒有用上抽象的知識(shí)。
2020-02-08
樓主,為啥我輸出的汽車名稱都是null啊
我找了一晚上都沒找到錯(cuò)哪了
2020-02-08
請(qǐng)問樓主
?
Car[]?cars是什么用法?數(shù)組不是得數(shù)據(jù)類型 【】數(shù)組名嗎?Car不是父類么?
2020-01-30
666
2020-01-13
太強(qiáng)了