java 抽象

MotoVehicle抽象类

package text1;

/*
 * 抽象
 */
public abstract class MotoVehicle {
    // 共同的属性
    private String no;// 车牌号
    private String brand;// 品牌2
    private String color;
    private double milage;

    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 String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getMilage() {
        return milage;
    }

    public void setMilage(double milage) {
        this.milage = milage;
    }

    public MotoVehicle() {// 无参构造
    }

    // 有参构造
    public MotoVehicle(String no, String brand, String color, int milage) {
        this.no = no;
        this.brand = brand;
        this.color = color;
        this.milage = milage;
    }

    public MotoVehicle(String no, String brand) {
        super();
        this.no = no;
        this.brand = brand;
    }

    public abstract void display();

    public abstract double calcRent(int days);
}
View Code

Bus类

package text1;

public class Bus extends MotoVehicle {
    private int seatCount;

    public Bus() {
        super();
    }

    public Bus(String no, String brand, String color, int milage, int seatCount) {
        super(no, brand, color, milage);
        // TODO Auto-generated constructor stub
        this.seatCount = seatCount;
    }

    public Bus(String no, String brand, int seatCount) {
        super(no, brand);
        // TODO Auto-generated constructor stub
        this.seatCount = seatCount;
    }

    public int getSeatCount() {
        return seatCount;
    }

    public void setSeatCount(int seatCount) {
        this.seatCount = seatCount;
    }

    public void display() {
        System.out.println("租车信息如下:");
        System.out.println("品牌:" + this.getBrand() + ",座位数:" + this.getSeatCount() + ",颜色:" + this.getColor() + ",车牌号:"
                + this.getNo() + ",里程数:" + this.getMilage());
    }

    public double calcRent(int days) {
        // TODO Auto-generated method stub
        double money = 0;
        if (seatCount <= 16) {
            money = 800;
        } else {
            money = 1500;
        }
        return money * days;
    }
}
View Code

Car类

package text1;

public class Car extends MotoVehicle {
    //自己特有的属性
    private String type;//类型
    
    //构造方法,继承父类构造方法
    public Car() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Car(String no, String brand, String color, int milage,String type) {
        super(no, brand, color, milage);
        // TODO Auto-generated constructor stub
        this.type=type;
    }

    public Car(String no,String brand,String type) {
        super(no,brand);
        // TODO Auto-generated constructor stub
        this.type=type;
    }
    


    //get set方法
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    //重写显示租车信息方法
    @Override
    public void display() {
        System.out.println("租车信息如下:");
        System.out.println("品牌:"+this.getBrand()+
                            ",类型:"+this.getType()+
                            ",颜色:"+this.getColor()+
                            ",车牌号:"+this.getNo()+
                            ",里程数:"+this.getMilage());
    }
    
    //重写父类计算租金方法
    @Override
    public double calcRent(int days) {
        // TODO Auto-generated method stub
        double money=0;
        if (type.equals("商务舱GL8")) {
            money=600;
        }else if (type.equals("550i")) {
            money=500;
        }else if (type.equals("林荫大道")) {
            money=300;
        }
        
        return money*days;
    }
}
View Code

Test

package text1;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //创建轿车对象
        Car car=new Car("豫A8888", "别克", "银灰色", 10000, "商务舱GL8");
        //调用显示信息方法
        car.display();
        //调用计算租金方法,结果由carPrice保存
        double carPrice=car.calcRent(2);
        System.out.println("总租金:"+carPrice);
        System.out.println("-----------------");
        
        //创建一个客车对象
        Bus bus=new Bus("豫B3336", "金杯", "白色", 15000, 50);
        bus.display();
        double busPrice=bus.calcRent(3);
        System.out.println("总租金:"+busPrice);
        
    }

}
View Code
原文地址:https://www.cnblogs.com/helloworld2019/p/10622030.html