创建了两个接口IPeopleCapacity(具有载客容量)、ICargoCapacity(具有载货容量);一个父类Vehicle代表车辆具有序号、名字、租用价钱属性,三个子类Car汽车(只载人)、Truck货车(只载货)、Pickup皮卡(能载人也能载货)。程序做了部分注释。希望可以多多交流。请多指教!
IPeopleCapacity.java
package com.imooc;
//创建接口IPeopleCapacity,具有载人容量
public interface IPeopleCapacity {
public int getPeopleCapacity();
} ICargoCapacity.java
package com.imooc;
//创建接口ICargoCapacity,具有载货容量
public interface ICargoCapacity {
public int getCargoCapacity();
} Vehicle.java
package com.imooc;
//创建父类Vehicle,子类将拥有父类所有的方法和属性
public class Vehicle {
public int id;
public String name;
public int rentPrice;
} Car.java
package com.imooc;
//创建子类Car汽车,继承父类Vehicle,且遵守接口IPeopleCapacity具有载人容量
public class Car extends Vehicle implements IPeopleCapacity{
public int peopleNum;
public Car(int id,String name,int rentPrice,int peopleNum) {
this.id=id;
this.name=name;
this.rentPrice=rentPrice;
this.peopleNum=peopleNum;
}
public String toString() {
return id+"t"+name+"t"+rentPrice+"元/天t"+peopleNum+"人";
}
@Override
public int getPeopleCapacity() {
// TODO Auto-generated method stub
return peopleNum;
}
} Truck.java
package com.imooc;
//创建子类Truck货车,继承父类Vehicle,且遵守接口ICargoCapacity具有载货容量
public class Truck extends Vehicle implements ICargoCapacity{
public int cargoWeight;
public Truck(int id,String name,int rentPrice,int cargoWeight) {
this.id=id;
this.name=name;
this.rentPrice=rentPrice;
this.cargoWeight=cargoWeight;
}
public String toString() {
return id+"t"+name+"t"+rentPrice+"元/天t"+cargoWeight+"吨";
}
@Override
public int getCargoCapacity() {
// TODO Auto-generated method stub
return cargoWeight;
}
} Pickup.java
package com.imooc;
//创建子类Pickup皮卡,继承父类Vehicle,且遵守接口IPeopleCapacity和ICargoCapacity具,有载人容量和载货容量
public class Pickup extends Vehicle implements ICargoCapacity,IPeopleCapacity{
public int peopleNum;
public int cargoWeight;
public Pickup(int id,String name,int rentPrice,int peopleNum,int cargoWeight) {
this.id=id;
this.name=name;
this.rentPrice=rentPrice;
this.peopleNum=peopleNum;
this.cargoWeight=cargoWeight;
}
public String toString() {
return id+"t"+name+"t"+rentPrice+"元/天t"+peopleNum+"人,"+cargoWeight+"吨";
}
@Override
public int getPeopleCapacity() {
// TODO Auto-generated method stub
return peopleNum;
}
@Override
public int getCargoCapacity() {
// TODO Auto-generated method stub
return cargoWeight;
}
} Test.java
package com.imooc;
import java.util.Scanner;
public class Test {
int rentSum=0;//总租金
int peopleSum=0;//总载客数
int cargoSum=0;//总载货数
String peopleVehicle="",cargoVehicle="";//所租用的载人车型和载客车型
public void method() {
// TODO Auto-generated method stub
//通过父类Vehicle的引用指向子类Car、Pickup、Truck的对象,存放在Vehicles[]数组中
Vehicle[] Vehicles= {new Car(1,"奥迪A4",500,4),
new Car(2,"马自达6",400,4),
new Pickup(3,"皮卡雪6",450,4,2),
new Car(4,"金龙",800,20),
new Truck(5,"松花江",400,4),
new Truck(6,"依维柯",1000,20)};
System.out.println("欢迎使用嗒嗒租车系统:");
System.out.println("您是否要租车:1.是;2.否");
Scanner input1=new Scanner(System.in);
switch(input1.nextInt()){
case 1:{
System.out.println("您可租用的类型及其价目表:"+"n"+"序号t"+"车型t"+"租金t"+"容量");
for(int i=0;i0) {
for(int j=1;j0) {
rentSum=rentSum*day;
System.out.println("****您的账单:n****您租用的可载人的车:"+peopleVehicle+"n****共可载客:"+peopleSum+"人");
System.out.println("****您租用的可载货的车:"+cargoVehicle+"n****共可载货:"+cargoSum+"吨");
System.out.println("****租车总价格:"+rentSum+"元");
System.out.println("n******感谢使用嗒嗒租车系统,祝您生活愉快!******");
System.exit(0);
} else{
System.out.println("输入的租车天数有误,系统无法验证!");
System.exit(0);}
}else {
System.out.println("输入的租车数量有误,系统无法验证!");
System.exit(0);
}
break;
}
case 2: {
System.out.println("******感谢使用嗒嗒租车系统,祝您生活愉快!******");
System.exit(0);
break;}
default:{
System.out.println("您输入的数字有误,系统无法验证!");
System.out.println("n******感谢使用嗒嗒租车系统,祝您生活愉快!******");
System.exit(0);}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test service=new Test();
service.method();
}
} 
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/27/viewspace-2799337/ ,如需转载,请注明出处,否则将追究法律责任。