/*
-
汽车类
/
public abstract class MotoVehicle {
/- 将成员变量封装隐藏起来,private 权限只在类内显示 / private String no;//车牌号 private String brand;//品牌 private int perRend;//日租金 public abstract float calRent(int days);//抽象方法 用来计算租金 public abstract void leaseoutFlow();//租车流程 /
- 使用set get方法实现对成员变量的存取,因为成员变量是private修饰的,只能在类内显示 */ public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPerRend() { return perRend; } public void setPerRend(int perRend) { this.perRend = perRend; } public MotoVehicle(){
}
public MotoVehicle(String no,String brand,int perRend){
this.no=no;
this.brand=brand;
this.perRend=perRend;
}
}
/* -
客车类
*/
public class Bus extends MotoVehicle{
private int seatCount;//座位数public int getSeatCount() {
return seatCount;
}public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
public Bus(){}
public Bus(String no,String brand,int perRent,int seatCount){
super(no,brand,perRent);//调用父类的构造函数
this.seatCount=seatCount;
}
/*- 重写父类 calRent方法 / public float calRent(int days){ return this.getPerRend()*days; } /
- 重写leaseoutFlow / public void leaseoutFlow(){ this.setPerRend(800); this.setNo("京80008000"); } } /
-
桥车类
*/
public class Car extends MotoVehicle{
private String type;//型号public String getType() {
return type;
}public void setType(String type) {
this.type = type;
}
public Car(){}
public Car(String no,String brand,int perRent,String type){
super(no,brand,perRent);//调用父类的构造函数
this.type=type;
}
/*- 重写父类 calRent方法 */ public float calRent(int days){ return this.getPerRend()*days; }
/*
- 重写leaseoutFlow / public void leaseoutFlow(){ this.setPerRend(600); this.setNo("京60006000"); } } /
汽车业务类
/
public class Motooperation {
public MotoVehicle MotoleaseOut(String MotoType){
MotoVehicle moto=null;;
if(MotoType.equals("桥车")){
moto=new Car();//多态转型 类型提升 向上转型 把子类提升为父类
moto.leaseoutFlow();
}else if(MotoType.equals("客车")){
moto=new Bus();//多态转型 类型提升 向上转型 把子类提升为父类
moto.leaseoutFlow();
}
return moto;
}
}
/汽车租赁管理类
*/
import java.util.Scanner;
public class RentMgrSys {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
Motooperation motoMrs=new Motooperation();
MotoVehicle moto=null;
System.out.println("欢迎来到汽车租赁公司!");
System.out.print("请输入要租赁的汽车类型: ");
System.out.print("1.桥车 2.客车");
int choose=input.nextInt();//汽车类型
String MotoType=null;
if(choose==1){
MotoType="桥车";
}else if(choose==2){
MotoType="客车";
}
moto=motoMrs.MotoleaseOut(MotoType);//获取租赁的汽车类型
System.out.print("请输入要租赁的天数");
int days=input.nextInt();
float money=moto.calRent(days);//租赁费用
System.out.println("分配给你的汽车牌号是:"+moto.getNo());
System.out.println("你需要支付的费用:"+money+"元");
input.close();
}
}
这句看不懂 float money=moto.calRent(days); moto是父类Motooperation的对象为什么调用的是子类的calRent()方法