亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

整理了一下,把多個對象保存起來,這樣后期如果需要統計載人量或者載貨量相關的也可以。

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){
????????//引導語
????????System.out.println("歡迎使用租車系統");
????????System.out.println("您是否要租車?1-是,0-否");
????????Scanner?input?=?new?Scanner(System.in);
????????int?isRent?=?input.nextInt();???????//是否租車
????????if?(isRent?==?1)?{
????????????System.out.println("您可租車的類型和價目表:");
????????????System.out.println("序號?汽車名稱?租金?容量");

????????????//聲明所有汽車并顯示
????????????Car[]?cars?=?{
????????????????????new?AManned("奧迪",500,?4),
????????????????????new?AManned("馬自達",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("請選擇要租車的數量:");
????????????int?carsCount?=?input.nextInt();
????????????Car[]?chooseCar?=?new?Car[carsCount];
????????????for?(int?j?=?0;?j?<?carsCount;?j++)?{
????????????????int?cycle?=?j+1;
????????????????System.out.println("請選擇第"+?cycle?+"輛車:");
????????????????int?carNum?=?input.nextInt();
????????????????chooseCar[j]?=?cars[carNum-1];
????????????}

????????????//選擇租車天數,并計算出租金
????????????System.out.println("請輸入要租的天數:");
????????????int?days?=?input.nextInt();
????????????double?singleSum?=?0;
????????????double?sum?=?0;
????????????for?(Car?aCar:?chooseCar)?{
????????????????singleSum?+=?aCar.getRent();
????????????}
????????????sum?=?singleSum?*?days;
????????????System.out.println("您的租金是:"+sum);

????????}
????}
}


正在回答

12 回答

AManned?car?=?(AManned)?cars[i];

樓主,這個是干什么的啊,沒看懂!

是調用數組里的對象嗎

0 回復 有任何疑惑可以回復我~
#1

Minenami

強制類型轉換
2020-01-14 回復 有任何疑惑可以回復我~

大佬,請收下我的膝蓋!

1 回復 有任何疑惑可以回復我~
首頁上一頁12下一頁尾頁

舉報

0/150
提交
取消

整理了一下,把多個對象保存起來,這樣后期如果需要統計載人量或者載貨量相關的也可以。

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號