就算先這樣吧,路過的大佬幫忙看看有沒有優化的地方_(:з」∠)_
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();
???? }
}
附運行結果
2019-03-17
請問一下,你的cars[chose_car].price 調用的是子類的price嗎?
按道理應該會產生多態,調用父類的price啊?
2019-03-02
一看到這么多相同顏色的字 我頭都大了 厲害了?
2019-02-22
大佬可以的,參考你的代碼幫我解決了很多問題
2019-02-11
emmm,第一次發表,沒想到格式怎么會變成這樣_(:з」∠)_