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

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

就算先這樣吧,路過的大佬幫忙看看有沒有優(yōu)化的地方_(:з」∠)_

Car.clas????????????????????????//父類、抽象類

package cars;

public abstract class Car {

???????protected double price; //定義價(jià)格

????????protected String carname; //定義名稱

????? ? public abstract String work();

}

MannedCar.class?????? //載人車子類

package cars;


public class MannedCar extends Car {

?????private int people; //定義乘坐人數(shù)

????public int getPeople() {

????????? ?return people;

????}


???? public void setPeople(int people) {

???? ????? this.people = people;

???? }

????

???? public MannedCar(String carname, double price, int people) {

???????? this.people = people;

???????? this.price = price;

???????? this.carname = carname;

???? }

????

???? @Override

???? public String work() {

???????? // TODO 自動(dòng)生成的方法存根

???????? return carname + "\t\t" + price + "/天" + "\t\t" + "載人:" + people + "人";

???? }

}

? CarryCar.class????????//貨車子類

package cars;


public class CarryCar extends Car {

???? private double weight; //定義載重量

???????? public double getWeight() {

???????? return weight;

???? }

????

???? public void setWeight(double weight) {

???????? this.weight = weight;

???? }

????

???? public CarryCar( String carname, double price, double weight) {

???????? this.weight = weight;

???????? this.price = price;

???????? this.carname = carname;

???? }

????

???? @Override

???? public String work() {

???????? // TODO 自動(dòng)生成的方法存根

???????? return carname + "\t\t" + price + "/天" + "\t\t" + "載貨:" + weight + "噸";

???? }

}

DualCar.class????????//載人/貨車 子類?? ?

? ?

package cars;


public class DualCar extends Car {

???? private int people; //定義乘坐人數(shù)

???? private double weight; //定義載重量

???? public int getPeople() {

???????? return people;

???? }

????

???? public void setPeople(int people) {

???????? this.people = people;

???? }

????

???? public double getWeight() {

???????? return weight;

???? }

????

???? public void setWeight(double wewight) {

???????? this.weight = wewight;

???? }

????

???? public DualCar( String carname, double price, int people, double weight) {

???????? this.people = people;

???????? this.weight = weight;

???????? this.price = price;

???????? this.carname = carname;

???? }

????

???? @Override

???? public String work() {

???????? // TODO 自動(dòng)生成的方法存根

???????? return carname + "\t\t" + price + "/天" + "\t\t" + "載貨:" + weight + "噸" + "\t\t" + "載人:" + people + "人";

???? }

}

BuyCar.class?????????//調(diào)試

package cars;

import java.util.Scanner;


public class BuyCar {

???? public static void main(String[] args) {

???????? Car[] cars = {

???????????? new MannedCar("奧德A4", 500, 6),

???????????? new MannedCar("大黃蜂", 950, 4),

???????????? new CarryCar("馬自達(dá)", 400, 1.2),

???????????? new CarryCar("金龍", 450, 1.6),

???????????? new DualCar("松花江", 400, 2, 1.6),

???????????? new DualCar("伊維特", 510, 4, 2.4),

???????? };

???????? Scanner scan = new Scanner(System.in);

???????? System.out.println("請(qǐng)選擇你的服務(wù): 0(退出), 1 (租車服務(wù))");

???????? int choice = scan.nextInt();

???????? if (choice == 1) {

???????????? System.out.println("**************可租用汽車表**************");

???????????? for (int i = 0; i < cars.length; i++) {

???????????? ???? System.out.println("序號(hào): " + i + "\t" + cars[i].work());

???????? }

???????? System.out.print("請(qǐng)輸入你要購買的數(shù)量: ");

???????? int num = scan.nextInt();

???????? // 判斷購買數(shù)量是否非法

???????? if (num <= 0) {

???????????? System.out.println("錯(cuò)誤!");

???????????? System.exit(0);

???????? }else{

???????????? int[] chose_cars = new int[num]; //存儲(chǔ)選擇的車輛序號(hào)數(shù)組

???????????? double allprice = 0; //存儲(chǔ)總價(jià)錢

???????????? for (int i = 0; i < num; i++) {

???????????? System.out.print("請(qǐng)輸入你要購買車輛的序號(hào): ");

???????????? int chose_car = scan.nextInt();

???????????? if (chose_car < 0 || chose_car >= cars.length) {

???????????? i--;

???????????? System.out.println("不存在此車輛");

???????????? continue;

???????? }else{

???????????? chose_cars[i] = chose_car;

???????? }

???? }

???? System.out.print("請(qǐng)輸入租借天數(shù): ");

???? int day = scan.nextInt();

???? System.out.println("**************您選擇的車輛列表**************");

???? for(int chose_car: chose_cars){

???? allprice += cars[chose_car].price * day; //計(jì)算總價(jià)錢

???? System.out.println("序號(hào): " + chose_car + "\t" +cars[chose_car].work());

???? }

???? System.out.println("您總共需要支付: " + allprice);

???? }

???? }else {

???????? System.out.println("您已退出服務(wù)!");

???????? System.exit(0);

???? }

???? scan.close();

???? }

}

附運(yùn)行結(jié)果

https://img1.sycdn.imooc.com//5c61906b0001ba5b08970445.jpg

正在回答

4 回答

請(qǐng)問一下,你的cars[chose_car].price 調(diào)用的是子類的price嗎?

按道理應(yīng)該會(huì)產(chǎn)生多態(tài),調(diào)用父類的price啊?

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

一看到這么多相同顏色的字 我頭都大了 厲害了?

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

大佬可以的,參考你的代碼幫我解決了很多問題

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

emmm,第一次發(fā)表,沒想到格式怎么會(huì)變成這樣_(:з」∠)_

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

舉報(bào)

0/150
提交
取消

就算先這樣吧,路過的大佬幫忙看看有沒有優(yōu)化的地方_(:з」∠)_

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

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

幫助反饋 APP下載

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

公眾號(hào)

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