设计了四个类:Cars、PickUp、Trucks和Vehicle类,其中Vehicle类是所有交通工具类的父类,Cars类和Trucks类继承自Vehicle类,PickUp类继承Cars类。通过简单的封装、继承和多态实现了面向对象的特性。
工程名:com.imooc 包名:dadarent
Vehicle类:
package dadarent;
public class Vehicle {
private int id;//序号
private String name;//汽车名称
private int rentMoney;//租金
private int passgerCapacity;//载人
private int cargoCapacity;//载货
}
Cars类,继承自Vehicle类,实现了封装操作。
package dadarent;
public class Cars extends Vehicle {
private int id;//序号
private String name;//汽车名称
private int rentMoney;//租金
private int passgerCapacity;//载人
private int cargoCapacity;//载货
public Cars(){} //重新定义有参构造方法时,好的习惯是重新加上无参构造方法,防止子类重新定义构造方法时因找不到父类相应的构造方法而出现错误。
public Cars(int id, String name, int rentMoney, int passCapacity ){
this.id=id;
this.name=name;
this.rentMoney=rentMoney;
this.passgerCapacity=passCapacity;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRentMoney() {
return rentMoney;
}
public void setRentMoney(int rentMoney) {
this.rentMoney = rentMoney;
}
public int getPassgerCapacity() {
return passgerCapacity;
}
public void setPassgerCapacity(int passgerCapacity) {
this.passgerCapacity = passgerCapacity;
}
public int getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
@Override //重写toString方法
public String toString() {
return id + "\t"+name + "\t"+rentMoney+"元/天"
+ "\t"+ "载人:"+passgerCapacity+"人" ;
}
}
Trucks类
package dadarent;
public class Trucks extends Vehicle {
int id;//序号
String name;//汽车名称
int rentMoney;//租金
int passgerCapacity;//载人
int cargoCapacity;//载货
public Trucks(){}
public Trucks(int id, String name, int rentmoney, int cargoCapacity ){
this.id=id;
this.name=name;
this.rentMoney=rentmoney;
this.cargoCapacity=cargoCapacity;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRentMoney() {
return rentMoney;
}
public void setRentMoney(int rentMoney) {
this.rentMoney = rentMoney;
}
public int getPassgerCapacity() {
return passgerCapacity;
}
public void setPassgerCapacity(int passgerCapacity) {
this.passgerCapacity = passgerCapacity;
}
public int getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
@Override //重写toString方法
public String toString() {
return id + "\t"+name + "\t"+rentMoney+"元/天"
+ "\t"+ "载货:"+cargoCapacity+"吨" ;
}
}
PickUp类,继承Cars类
package dadarent;
public class PickUp extends Cars {
public PickUp(int id, String name, int rentmoney, int passcapacity, int cargoCapacity ){
this.setId(id);
this.setName(name);
this.setRentMoney(rentmoney);
this.setPassgerCapacity(passcapacity);
this.setCargoCapacity(cargoCapacity);
}
@Override //重写toString方法
public String toString() {
return this.getId() + "\t"+this.getName() + "\t"+this.getRentMoney()+"元/天"
+ "\t"+ "载人:"+this.getPassgerCapacity()+"人"+"载货:"+this.getCargoCapacity()+"吨" ;
}
}
最后加上main方法,其中涉及基本类型与字符串之间的转换(加上空字符串即可)。
package dadarent;
import java.util.Scanner;
public class TestVehicle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("欢迎使用达达租车系统:\n您是否要租车: 1 是 2 否");
int choice=input.nextInt();//此处choice表示是否进入达达租车系统
if(choice==1)
{
//通过构造函数输入各车型数据
Cars audi=new Cars(1,"奥迪A4",500,4);//通过构造函数进行赋值操作。不要使用 Cars audi=new Cars(); audi.Cars(1,"奥迪A4",500,4,0);
//的方式进行赋值,因为那样调用的是无参构造方法。
Cars mazda=new Cars(2,"马自达6",400,4);
PickUp chevrolet=new PickUp(3,"皮卡雪6",450,4,2);
Cars goldenDragon=new Cars(4,"金龙",800,20);
Trucks songHuaJiang =new Trucks(5,"松花江",400,4);
Trucks naveco =new Trucks(6,"依维柯",1000,20);
//输出车辆信息列表
System.out.println("您可租车的类型及其价目表:\n 序号\t汽车名称\t租金\t容量");
System.out.println(audi);
System.out.println(mazda);
System.out.println(chevrolet);
System.out.println(goldenDragon);
System.out.println(songHuaJiang);
System.out.println(naveco);
System.out.println("请输入您要租汽车的数量:");
int rentNum=input.nextInt();
int[] storeRentNum={0,0,0,0,0,0}; //用于存储租车序号
for(int i=0;i<rentNum;i++)
{
System.out.println("请输入第"+(i+1)+"辆车的序号");
storeRentNum[i]=input.nextInt();
}
System.out.println("请输入租车天数:");
int rentTime=input.nextInt();
String displayPassenger="";//输出载人车辆
String displayCargo="";//输出载货车辆
int sumPassenger=0;//输出载人数量
int sumCargo=0;//输出载货数量
int sumRent=0;//输出花费总金额
for(int i=0;i<rentNum;i++)
{
switch(storeRentNum[i]){
case 1:displayPassenger+=audi.getName()+"\t";
sumPassenger+=audi.getPassgerCapacity();
sumRent+=(rentTime*audi.getRentMoney());
break;
case 2:displayPassenger+=mazda.getName()+"\t";
sumPassenger+=mazda.getPassgerCapacity();
sumRent+=(rentTime*mazda.getRentMoney());
break;
case 3:displayPassenger+=chevrolet.getName()+"\t";
displayCargo+=chevrolet.getName()+"\t";
sumPassenger+=chevrolet.getPassgerCapacity();
sumCargo+=chevrolet.getCargoCapacity();
sumRent+=(rentTime*chevrolet.getRentMoney());
break;
case 4:displayPassenger+=goldenDragon.getName()+"\t";
sumPassenger+=goldenDragon.getPassgerCapacity();
sumRent+=(rentTime*goldenDragon.getRentMoney());
break;
case 5:displayCargo+=(songHuaJiang.getName()+"\t");
sumCargo+=songHuaJiang.getCargoCapacity();
sumRent+=(rentTime*songHuaJiang.getRentMoney());
break;
case 6: displayCargo+=(naveco.getName()+"\t");
sumCargo+=naveco.getCargoCapacity();
sumRent+=(rentTime*naveco.getRentMoney());
break;
default: System.out.println("本公司暂不支持该车型,请重新输入");
break;
}
}
//输出模块
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
System.out.println(displayPassenger+"共载人:"+sumPassenger+"人");
System.out.println("***可载货的车有:");
System.out.println(displayCargo+"共载货:"+sumCargo+"吨");
System.out.println("租车总价格:"+sumRent);
}
else{
System.out.println("欢迎下次使用");
}
}
}
热门评论
这么写 会报错吗?我写的报错了,继承cars类的时候