import java.util.Scanner; class Car{ private int carNumber;//车的编号 private String carName;//车名 private int carRentMoney;//租金 private int peoCapa;//载客量 private double goodCapa;//载货量 public int getCarNumber() { return carNumber; } public void setCarNumber(int carNumber) { this.carNumber = carNumber; } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getCarRentMoney() { return carRentMoney; } public void setCarRentMoney(int carRentMoney) { this.carRentMoney = carRentMoney; } public int getPeoCapa() { return peoCapa; } public void setPeoCapa(int peoCapa) { this.peoCapa = peoCapa; } public double getGoodCapa() { return goodCapa; } public void setGoodCapa(double goodCapa) { this.goodCapa = goodCapa; } } class Truck extends Car { //构造代码块,为属性赋值 { setCarNumber(5);setCarName("松花江");setCarRentMoney(400);setGoodCapa(4); } int carNumber1 = getCarNumber();String carName1 = getCarName(); int carRentMoney1 = getCarRentMoney();double goodCapa1 = getGoodCapa(); { setCarNumber(6);setCarName("依维河");setCarRentMoney(1000);setGoodCapa(20); } int carNumber2 = getCarNumber();String carName2 = getCarName(); int carRentMoney2 = getCarRentMoney();double goodCapa2 = getGoodCapa(); } class PiKup extends Car { { setCarNumber(3);setCarName("皮卡雪6");setCarRentMoney(400);setPeoCapa(4);setGoodCapa(2); } int carNumber1 = getCarNumber();String carName1 = getCarName(); int carRentMoney1 = getCarRentMoney();double goodCapa1 = getGoodCapa();int peoCapa1 = getPeoCapa(); } class PassengerCar extends Car { { setCarNumber(1);setCarName("奥迪A4");setCarRentMoney(500);setPeoCapa(4); } int carNumber1 = getCarNumber();String carName1 = getCarName(); int carRentMoney1 = getCarRentMoney();int peoCapa1 = getPeoCapa(); { setCarNumber(2);setCarName("马自达");setCarRentMoney(400);setPeoCapa(4); } int carNumber2 = getCarNumber();String carName2 = getCarName(); int carRentMoney2 = getCarRentMoney();int peoCapa2 = getPeoCapa(); { setCarNumber(4);setCarName("金龙");setCarRentMoney(800);setPeoCapa(20); } int carNumber3 = getCarNumber();String carName3 = getCarName(); int carRentMoney3 = getCarRentMoney();int peoCapa3 = getPeoCapa(); } class RentCarTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车:1.是 0.否"); int x = sc.nextInt(); if(x==0){ System.out.println("欢迎下次使用"); }else if(x==1){ Truck t = new Truck(); PiKup p = new PiKup(); PassengerCar pa = new PassengerCar(); //顾客选择页面显示 System.out.println("您可租车的类型及其价目表如下:"); System.out.println("序号"+" 汽车名称"+" 租金"+" 容量"); System.out.println(pa.carNumber1+". "+pa.carName1+" "+pa.carRentMoney1+"元/天"+" 载人:"+pa.peoCapa1+"人"); System.out.println(pa.carNumber2+". "+pa.carName2+" "+pa.carRentMoney2+"元/天"+" 载人:"+pa.peoCapa2+"人"); System.out.println(p.carNumber1+". "+p.carName1+" "+p.carRentMoney1+"元/天"+" 载人:"+p.peoCapa1+"人"+" 载货:"+p.goodCapa1+"吨"); System.out.println(pa.carNumber3+". "+pa.carName3+" "+pa.carRentMoney3+"元/天"+" 载人:"+pa.peoCapa3+"人"); System.out.println(t.carNumber1+". "+t.carName1+" "+t.carRentMoney1+"元/天"+" 载人:"+t.goodCapa1+"吨"); System.out.println(t.carNumber2+". "+t.carName2+" "+t.carRentMoney2+"元/天"+" 载人:"+t.goodCapa2+"吨"); System.out.println("请输入您要租车的数量:"); int y=sc.nextInt(); int[] array = new int[y]; for (int i=0;i<y ;i++ ) { System.out.println("请输入第"+(i+1)+"辆汽车的序号(1——6):"); int ar = sc.nextInt(); if(ar<1||ar>6){ System.out.println("您输入的序号不在列表范围内,请重新输入序号:"); array[i] = sc.nextInt(); }else{ array[i] = ar; } } System.out.println("请输入您要租车的天数:"); int day = sc.nextInt(); //生成账单 int sumMoney = 0;//总金额 int sumPeoCapa = 0;//载客量 double sumGoodCapa = 0;//载货量 for (int i=0;i<y ;i++ ) { switch(array[i]) { case 1: sumMoney +=pa.carRentMoney1*day; sumPeoCapa +=pa.peoCapa1; break; case 2: sumMoney +=pa.carRentMoney2*day; sumPeoCapa +=pa.peoCapa2; break; case 3: sumMoney +=p.carRentMoney1*day; sumPeoCapa +=p.peoCapa1; sumGoodCapa +=p.goodCapa1; break; case 4: sumMoney +=pa.carRentMoney3*day; sumPeoCapa +=pa.peoCapa3; break; case 5: sumMoney +=t.carRentMoney1*day; sumGoodCapa +=t.goodCapa1; break; case 6: sumMoney +=t.carRentMoney2*day; sumGoodCapa +=t.goodCapa2; break; default: break; } } System.out.println("您的账单如下:"); System.out.println("您租车所需的总金额是:"+sumMoney); System.out.println("您租的汽车一共可以载"+sumPeoCapa+"人、"+sumGoodCapa+"吨货"); } } }
写了代码,但是在写的时候,不太清楚父类和子类一般应该写什么内容,所以就在父类里面定义了汽车的属性,各个子类中为属性赋值,最终还是通过创建子类对象来访问的属性。其他的内容都是在main方法中写的。感觉没有发挥继承的优势,大家给点建议呢。
http://www.imooc.com/qadetail/44278 看这个吧,下面也有大神的评论,相对比叫完善,你可以根据他的代码再加上大神的评论然后修改成自己的代码