需求说明:某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。
出租车型及其信息如下:
汽车类(抽象类)
属性:
(1)品牌(brand)
(2)日租金(dayRent)
(3)车牌号(vehicleNum)
方法:抽象类方法(计算总租金):calRent(int days)
代码如下:
public abstract class Vehicle {
//汽车类的属性(由于轿车、客车要继承汽车这个抽象,故属性要设置为public,默认为public static final)
//汽车品牌
String brand;
//车牌号
String vehicleNum;
//日租金
int dayRent;
//汽车类的带参构造
public Vehicle(String brand, String vehicleNum, int dayRent) {
this.brand = brand;
this.vehicleNum = vehicleNum;
this.dayRent = dayRent;
}
//计算总租金的方法
public abstract double calRent(int days);
}
轿车类(继承汽车类)
属性(轿车特有的):型号(carType)
宝马 ->x6/550i
别克->林荫达到/GL8
方法:实现汽车类的计算总租金的方法
代码如下:
public class Cars extends Vehicle{
//轿车类(特有属性:型号)
String carsType;
//轿车的有参构造方法
public Cars(String brand, String vehicleNum, int dayRent,String carsType) {
super(brand, vehicleNum, dayRent);
this.carsType=carsType;
}
@Override
public double calRent(int days) {
double rent=dayRent*days;
if (days>150){
rent*=0.7;
}else if (days>30){
rent*=0.8;
}else if (days>7){
rent*=0.9;
}
return rent;
}
}
客车类(继承汽车类)
属性(客车特有的):座位(coachSeat)
金杯->16座/34座
金龙->16座/34座
方法:实现汽车类的计算总租金的方法
代码如下:
public class Coach extends Vehicle{
//客车(特有属性:座位数)
String coachSeat;
//客车的有参构造
public Coach(String brand, String vehicleNum, int dayRent,String coachSeat) {
super(brand, vehicleNum, dayRent);
this.coachSeat=coachSeat;
}
@Override
public double calRent(int days) {
double rent= dayRent*days;
if