第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

整理了一下,把多個(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);

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


正在回答

12 回答

for?(Car?aCar:?chooseCar)

這里的Car aCar是什么,這個(gè)循環(huán)怎么看,這知識(shí)點(diǎn)不會(huì)啊,“:”是什么意思啊,大神求解答

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕移動(dòng)7175383

用foreach函數(shù)把chooseCar數(shù)組遍歷了一遍來求單日租金和。
2020-05-13 回復(fù) 有任何疑惑可以回復(fù)我~

很強(qiáng),看完之后深受啟發(fā),感謝

0 回復(fù) 有任何疑惑可以回復(fù)我~

樓主,我想問一下,你那個(gè)強(qiáng)制引用類型轉(zhuǎn)換的意義在哪?我在想不用強(qiáng)制類型轉(zhuǎn)換,用cars[i].方法這樣形式去操作也行啊,麻煩解答一下謝謝。

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

你不愛的寶寶

老哥你這個(gè)思路給的很及時(shí)啊,能減少代碼復(fù)雜度,看起來更加明朗了
2020-02-15 回復(fù) 有任何疑惑可以回復(fù)我~
#2

qq__8737

不行啊,cars[i].只能調(diào)用Car類的方法,調(diào)用不了子類的方法。你怎么弄的啊
2020-03-27 回復(fù) 有任何疑惑可以回復(fù)我~

厲害?!

0 回復(fù) 有任何疑惑可以回復(fù)我~

厲害?!

0 回復(fù) 有任何疑惑可以回復(fù)我~

感覺這一季的大多知識(shí)都用上了,就是沒有用上抽象的知識(shí)。

0 回復(fù) 有任何疑惑可以回復(fù)我~

樓主,為啥我輸出的汽車名稱都是null啊
我找了一晚上都沒找到錯(cuò)哪了

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_狼狽_0

代碼都沒給出來,怎么知道哪里錯(cuò)了。
2020-02-11 回復(fù) 有任何疑惑可以回復(fù)我~
#2

慕村7056548

我和你一樣,你看下在Car的類里有沒有初始化carName
2020-03-21 回復(fù) 有任何疑惑可以回復(fù)我~

請(qǐng)問樓主?Car[]?cars是什么用法?數(shù)組不是得數(shù)據(jù)類型 【】數(shù)組名嗎?Car不是父類么?

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_狼狽_0

這是多態(tài)中的引用多態(tài)和數(shù)組,cars是數(shù)組名。這個(gè)引用多態(tài)是父類的引用指向子類的對(duì)象,其中數(shù)組列表里面全是子類,一個(gè)個(gè)進(jìn)行父類的引用指向子類的對(duì)象。
2020-02-11 回復(fù) 有任何疑惑可以回復(fù)我~
#2

JAVA新小白 回復(fù) qq_狼狽_0

噢,懂了,謝謝大神
2020-02-12 回復(fù) 有任何疑惑可以回復(fù)我~

666

0 回復(fù) 有任何疑惑可以回復(fù)我~

太強(qiáng)了


2 回復(fù) 有任何疑惑可以回復(fù)我~
首頁上一頁12下一頁尾頁

舉報(bào)

0/150
提交
取消

整理了一下,把多個(gè)對(duì)象保存起來,這樣后期如果需要統(tǒng)計(jì)載人量或者載貨量相關(guān)的也可以。

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)