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

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

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

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

package cars;

public abstract class Car {

???????protected double price; //定義價格

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

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

}

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

package cars;


public class MannedCar extends Car {

?????private int people; //定義乘坐人數

????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 自動生成的方法存根

???????? 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 自動生成的方法存根

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

???? }

}

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

? ?

package cars;


public class DualCar extends Car {

???? private int people; //定義乘坐人數

???? 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 自動生成的方法存根

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

???? }

}

BuyCar.class?????????//調試

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("馬自達", 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("請選擇你的服務: 0(退出), 1 (租車服務)");

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

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

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

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

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

???????? }

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

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

???????? // 判斷購買數量是否非法

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

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

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

???????? }else{

???????????? int[] chose_cars = new int[num]; //存儲選擇的車輛序號數組

???????????? double allprice = 0; //存儲總價錢

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

???????????? System.out.print("請輸入你要購買車輛的序號: ");

???????????? 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("請輸入租借天數: ");

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

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

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

???? allprice += cars[chose_car].price * day; //計算總價錢

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

???? }

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

???? }

???? }else {

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

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

???? }

???? scan.close();

???? }

}

附運行結果

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

正在回答

4 回答

請問一下,你的cars[chose_car].price 調用的是子類的price嗎?

按道理應該會產生多態,調用父類的price啊?

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

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

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

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

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

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

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

舉報

0/150
提交
取消

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

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

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

幫助反饋 APP下載

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

公眾號

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