就算先這樣吧,路過的大佬幫忙看看有沒有優(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é)果
2019-03-17
請(qǐng)問一下,你的cars[chose_car].price 調(diào)用的是子類的price嗎?
按道理應(yīng)該會(huì)產(chǎn)生多態(tài),調(diào)用父類的price啊?
2019-03-02
一看到這么多相同顏色的字 我頭都大了 厲害了?
2019-02-22
大佬可以的,參考你的代碼幫我解決了很多問題
2019-02-11
emmm,第一次發(fā)表,沒想到格式怎么會(huì)變成這樣_(:з」∠)_