慕哥5629723
2019-03-07 15:10
//父类 public abstract class Car { //使用封装概念,设置属性 private String name; private double rent; private int Manned; private double Cargo; //使用set、get方法读写属性 public int getManned() { return Manned; } public void setManned(int manned) { Manned = manned; } public double getCargo() { return Cargo; } public void setCargo(double cargo) { Cargo = cargo; } public double getRent() { return rent; } public void setRent(double rent) { this.rent = rent; } public String getName() { return name; } public void setName(String name) { this.name = name; } //继承父类 public class PassengerCar extends Car { //使用this方法 public PassengerCar(String name, double rent,int manned) { this.setName(name); this.setRent(rent); this.setManned(manned); } public String toString() { return this.getName()+"\t"+this.getRent()+"/天"+"\t"+"可乘坐:"+this.getManned()+"人"; } //继承父类 public class CargoCapacity extends Car { public CargoCapacity(String name, double rent,double Cargo) { this.setName(name); this.setRent(rent); this.setCargo(Cargo); } public String toString() { return this.getName()+"\t"+this.getRent()+"/天"+"\t"+"可载货:"+this.getCargo()+"吨"; } //继承父类 public class Pickup extends Car { public Pickup(String name, double rent, int manned,double cargo) { this.setName(name); this.setRent(rent); this.setManned(manned); this.setCargo(cargo); } public String toString() { return this.getName()+"\t"+this.getRent()+"/天"+"\t"+"可乘坐"+this.getManned()+"人,"+"载货"+this.getCargo()+"吨"; } //测试类 public class test { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("欢迎使用答答租车系统"); System.out.println("您是否要租车: 1是 0否"); Car[] au = {new PassengerCar("奥迪A8L",800,1),new PassengerCar("大金龙",1000,1), new CargoCapacity("福田欧曼",1200,5),new CargoCapacity("一汽解放",950,40), new Pickup("皮卡",1500,1,2)}; Scanner sc = new Scanner(System.in); String input = sc.nextLine(); if(input.equals("1")) { System.out.println("您可租车的类型及其价目表"); System.out.println("序号\t汽车名称\t租金\t\t容量"); for(int i = 0; i<au.length; i++) { System.out.println((i+1)+"\t"+au[i]); } }else if(input != "1") { System.out.println("感谢您使用答答租车系统,祝您生活愉快!!"); } System.out.println("请您输入需要租赁汽车的数量:"); int carcount = sc.nextInt(); //更新车辆数组 Car[] newcar=new Car[carcount]; double bills=0; int zren=0; double zhuo=0; for(int i=0;i<carcount;i++) { System.out.println("请您输入第"+(i+1)+"辆车的序号:"); int carnum=sc.nextInt(); newcar[i] = au[carnum-1]; } System.out.println("请你输入租赁的天数:"); int day=sc.nextInt(); //计算租金 for(int i=0;i<carcount;i++) { bills=bills+newcar[i].getRent()*day; } //输出账单 System.out.println("你的账单"); //列出所选乘用车并计算总的载人数 System.out.println("*****可载人的车有"); for(int i=0;i<carcount;i++) { if(newcar[i].getManned() != 0) { zren += newcar[i].getManned(); System.out.println(newcar[i]); } } System.out.println("*****可载货的车有"); for(int i = 0 ;i<carcount;i++) { if(newcar[i].getCargo() != 0) { zhuo +=newcar[i].getCargo(); System.out.println(newcar[i]); } } System.out.println("共可载客"+zren+"人"); System.out.println("共可载货"+zhuo+"吨"); System.out.println("请付租金"+bills+"元"); }
你这个子类toString方法里面没必要用this吧,我记得this不是要静态类或者变量才可以调用,难道我记错了吗?
public static void main(String[] args) {
// TODO Auto-generated method stub
//初始化,输入拥有的车的信息
Vehicle[] vehs =new Vehicle[5];
vehs[0] = new Truck("mo1", 100, 10);
vehs[1] = new Truck("mo2", 200, 20);
vehs[2] = new Truck("mo3", 300, 30);
vehs[3] = new Car("car1", 1000, 10);
vehs[4] = new Car("car2", 2000, 20);
请问怎么才能像你那样把代码分行显示出来?
有个问题,如果刚开始 “您是否要租车: 1是 0否 ”后面用户输入的数字不等于1的话,程序还会继续执行下去。
不错哦
Java入门第二季 升级版
530671 学习 · 6091 问题
相似问题
回答 3
回答 4