慕粉3170877
2016-04-29 17:20
package ddsy; //定义父类 public abstract class Auto { //定义属性 private String carName;//定义车型名称 private double price;//定义每日租金 private int busLoad;//定义车的载客量 private int loadWeight;//定义车的载货量 public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getBusLoad() { return busLoad; } public void setBusLoad(int busLoad) { this.busLoad = busLoad; } public int getLoadWeight() { return loadWeight; } public void setLoadWeight(int loadWeight) { this.loadWeight = loadWeight; } } //创建子类载人的车 package ddsy; public class Passengerauto extends Auto { //构造方法用来初始化对象 public Passengerauto(String newcarName, double newPrice, int newbusLoad,int newloadWeight){ this.setCarName(newcarName); this.setPrice(newPrice); this.setBusLoad(newbusLoad); this.setLoadWeight(newloadWeight); } } //创建子类载货的车 package ddsy; public class Truck extends Auto { public Truck(String newcarName, double newprice, int newbusLoad,int newloadWeight){ this.setCarName(newcarName); this.setPrice(newprice); this.setBusLoad(newbusLoad); this.setLoadWeight(newloadWeight); } } //创建了子类皮卡车 package ddsy; public class Pickup extends Auto { public Pickup(String newcarName, double newPrice, int newbusLoad, int newloadWeight){ this.setCarName(newcarName); this.setPrice(newPrice); this.setBusLoad(newbusLoad); this.setLoadWeight(newloadWeight); } } //答答租车系统主函数 package ddsy; import java.util.Scanner; public class Initial { public static void main(String[] args) { // TODO Auto-generated method stub Passengerauto car1 = new Passengerauto("奥迪A4 ",500,4,0); Passengerauto car2 = new Passengerauto("马自达6 ",400,4,0); Pickup car3= new Pickup("皮卡雪6",450,4,2); Passengerauto car4 = new Passengerauto("金龙 ",800,20,0); Truck car5 = new Truck("松花江",400,0,4); Truck car6 = new Truck("依维柯",1000,0,20); Scanner input=new Scanner(System.in);//创建Scanner对象 System.out.println("欢迎使用答答租车系统:您是否要租车:1是 0否"); int choiceNum=input.nextInt(); if(choiceNum==0){ System.out.println("请您退出答答租车系统!!!"); }else{ System.out.println("您可租车的类型及价目表:"); System.out.println("序号 \t汽车名称\t租金\t\t载客容量\t载货容量"); // for(Auto currentAuto:autosystem) //System.out.println(1+"\t"+currentAuto.getCarName()+"\t"+currentAuto.getPrice()+"\t"+currentAuto.getBusLoad()+"\t"+currentAuto.getLoadWeight()); System.out.println(1+"\t"+car1.getCarName()+"\t"+car1.getPrice()+"元/天"+"\t"+car1.getBusLoad()+"人"+"\t"+car1.getLoadWeight()+"吨"); System.out.println(2+"\t"+car2.getCarName()+"\t"+car2.getPrice()+"元/天"+"\t"+car2.getBusLoad()+"人"+"\t"+car2.getLoadWeight()+"吨"); System.out.println(3+"\t"+car3.getCarName()+"\t"+car3.getPrice()+"元/天"+"\t"+car3.getBusLoad()+"人"+"\t"+car3.getLoadWeight()+"吨"); System.out.println(4+"\t"+car4.getCarName()+"\t"+car4.getPrice()+"元/天"+"\t"+car4.getBusLoad()+"人"+"\t"+car4.getLoadWeight()+"吨"); System.out.println(5+"\t"+car5.getCarName()+"\t"+car5.getPrice()+"元/天"+"\t"+car5.getBusLoad()+"人"+"\t"+car5.getLoadWeight()+"吨"); System.out.println(6+"\t"+car6.getCarName()+"\t"+car6.getPrice()+"元/天"+"\t"+car6.getBusLoad()+"人"+"\t"+car6.getLoadWeight()+"吨"); System.out.println("请您输入要租车的数量:"); int autonum=input.nextInt(); float msum=0f; int psum=0; int tsum=0; System.out.println("请输入第1辆车的数量:"); int num1=input.nextInt(); msum+=num1*500; psum+=num1*4; System.out.println("请输入第2辆车的数量:"); int num2=input.nextInt(); msum+=num2*400; psum+=num2*4; System.out.println("请输入第3辆车的数量:"); int num3=input.nextInt(); msum+=num3*450; psum+=num3*4; tsum+=num3*2; System.out.println("请输入第4辆车的数量:"); int num4=input.nextInt(); msum+=num4*800; psum+=num4*20; System.out.println("请输入第5辆车的数量:"); int num5=input.nextInt(); msum+=num5*400; tsum+=num5*4; System.out.println("请输入第6辆车的数量:"); int num6=input.nextInt(); msum+=num6*1000; tsum+=num6*20; System.out.println("请输入租车天数:"); int time=input.nextInt(); System.out.println("您的账单:"); if(num1!=0){ System.out.println("***可载人的车有:"); System.out.print(car1.getCarName()+"\t"); } if(num2!=0){ System.out.print(car2.getCarName()+"\t"); } if(num3!=0){ System.out.print(car3.getCarName()+"\t"); } if(num4!=0){ System.out.print(car4.getCarName()+"\t"); } System.out.println("共载人"+psum+"人"); System.out.println("***可载货的车有:"); if(num3!=0){ System.out.print(car3.getCarName()+"\t"); } if(num5!=0){ System.out.print(car5.getCarName()+"\t"); } if(num6!=0){ System.out.print(car1.getCarName()+"\t"); } System.out.println("共载货"+tsum+"吨"); msum=msum*time; System.out.println("租车总价格"+msum+"元"); } } }
想问下在你的程序中,子类为什么没有定义成员变量,虽然继承了父类,但是父类的成员变量是private类型的,子类是不可以继承的啊?为什么子类却继承了成员变量呢?
写的不错,换种有接口的更好了
非常可以
你这个代码有点BUG,车辆数目那里。
真心不错好长
不错,够长的
Java入门第二季 升级版
530653 学习 · 6091 问题
相似问题