问答详情
源自:12-1 综合练习

就算先这样吧,路过的大佬帮忙看看有没有优化的地方_(:з」∠)_

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.mukewang.com/5c61906b0001ba5b08970445.jpg

提问者:慕瓜8538410 2019-02-11 23:10

个回答

  • qq_慕仔212396
    2019-03-17 17:36:53

    请问一下,你的cars[chose_car].price 调用的是子类的price吗?

    按道理应该会产生多态,调用父类的price啊?

  • Dusdii
    2019-03-02 22:57:43

    一看到这么多相同颜色的字 我头都大了 厉害了 

  • 无能狂怒人
    2019-02-22 16:24:54

    大佬可以的,参考你的代码帮我解决了很多问题

  • 慕瓜8538410
    2019-02-11 23:12:23

    emmm,第一次发表,没想到格式怎么会变成这样_(:з」∠)_