父类:MotoVehicle
public abstract class MotoVehicle {
String no;
String brand;
String color;
String mileage;
int days;
public abstract float CalcRent(int days); }
子类1:Car
public final class Car extends MotoVehicle {
String type;
@Override
public float CalcRent(int days) {
float rent = 0;
if(type.equals("别克商务舱GL8")){
rent = 600*days;
}else if(type.equals("宝马550i")){
rent = 500*days;
}else if(type.equals("别克林荫大道")){
rent = 300*days;
}
return rent;
}
public Car() {
super();
}
public Car(String type) {
super();
this.type = type;
}
}
子类2:Bus
public final class Bus extends MotoVehicle {
int seatcount;
@Override
public float CalcRent(int days) {
float rent = 0;
if(seatcount<=16){
rent = 800*days;
}else{
rent = 1500*days;
}
return rent;
}
public Bus() {
super();
}
public Bus(int seatcount) {
super();
this.seatcount = seatcount;
}
}
测试类:MotoVehicleText
import java.util.Scanner;
public class MotoVehicleText {
public static void main(String[] args) {
String kind;
float rent = 0;
Scanner input = new Scanner(System.in);
System.out.println("请输入租车的种类");
kind = input.next();
if(kind.equals("car")){
Car a = new Car();
System.out.println("请输入租车时间:");
a.days = input.nextInt();
System.out.println("请输入租车的型号:");
a.type = input.next();
rent = a.CalcRent(a.days);
}else if(kind.equals("bus")){
Bus b = new Bus();
System.out.println("请输入租车时间:");
b.days = input.nextInt();
System.out.println("请输入租车的座位数:");
b.seatcount = input.nextInt();
rent = b.CalcRent(b.days);
}
System.out.println("您的租车费用为"+rent);
input.close();
}
}