1.创建租金lease类
(1)创建车的对象,放在数组中
MotoVehicle[] motos = new MotoVehicle[4];
motos[0] = new Car(“宝马550i”,“京NY28588”);
motos[1] = new Car(“宝马550i”,“京NNN328”);
motos[2] = new Car(“别克林荫大道”,“京NY28588”);
motos[3] = new Bus(“金龙”,34);
(2) 循环调用calcRent()方法,计算总租金
int total = 0;
for(int i = 0; i <motos.length; i++)
{
total += motos[i].calRent(days);
}
System.out.println(“总租金” + total);
2.创建MotoVehicle类,实现calcRent ()方法 (父类)
public abstract class MotoVehicle {
public static int length;
private String number; //车牌号
private String brand; //品牌
abstract int calRent(int days); //计算租金
public MotoVehicle()
{
}
public MotoVehicle(String number, String brand)
{
this.number = number;
this.brand = brand;
}
public void setNumber(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getBrand() {
return brand;
}
}
3.汽车类 (子类)
public abstract class MotoVehicle {
public static int length;
private String number; //车牌号
private String brand; //品牌
abstract int calRent(int days); //计算租金
public MotoVehicle()
{
}
public MotoVehicle(String number, String brand)
{
this.number = number;
this.brand = brand;
}
public void setNumber(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getBrand() {
return brand;
}
}
4.卡车类 (子类)
public class Bus extends MotoVehicle{
private int dunwei; //吨位
private String name;
public Bus(String name,int dunwei)
{
this.name = name;
this.dunwei = dunwei;
}
@Override
int calRent(int days) {
return 50*dunwei*days;
}
}