首页 > 编程语言 > 详细

Java练习——抽象类

时间:2021-02-07 23:10:56      阅读:26      评论:0      收藏:0      [点我收藏+]

需求:

2辆宝马,1辆别克商务舱,1辆金龙(34)座,租5天共多少租金。

  轿车 客车(金杯、金龙)
车型 别克商务舱GL8 宝马550i 别克林荫大道 <=16座 >16座
日租费(元/天) 600 500 300 800 1500

新购置了卡车,根据吨位,租金每吨每天50,对系统进行扩展,计算汽车租赁的总租金

实现思路:

类:MotoVehicle(机动车,抽象类),Car,Bus

类中的属性:

MotoVehicle{No(车牌号),Brand(品牌),Color(颜色),Mileage(里程)}

Car{Type(型号),TypeRent(租车的数量*车型日租费)}

Bus{SeatCount(乘载数量),TypeRent(租车的数量*车型日租费)}

实现代码:

MotoVehicle.java

 

package com.sbx.ex5;

/**
 * Created by BlueLover
 * Name:苏半夏
 * Date:2021/2/7
 * Time:16:36
 */
public abstract class MotoVehicle {
    private int No;//数量
    private String Brand;//品牌
    private String Color;//颜色
    private String Mileage;//里程
    public abstract int CalcRent(int days);

    public int getNo() {
        return No;
    }

    public void setNo(int no) {
        No = no;
    }

    public String getBrand() {
        return Brand;
    }

    public void setBrand(String brand) {
        Brand = brand;
    }

    public String getColor() {
        return Color;
    }

    public void setColor(String color) {
        Color = color;
    }

    public String getMileage() {
        return Mileage;
    }

    public void setMileage(String mileage) {
        Mileage = mileage;
    }
}

 

Car.java

package com.sbx.ex5;

/**
 * Created by BlueLover
 * Name:苏半夏
 * Date:2021/2/7
 * Time:16:38
 */
public final class Car extends MotoVehicle{
    private String Type;//型号
    private int TypeRent;//租车的数量*车型日租费

    public int getTypeRent() {
        return TypeRent;
    }

    public void setTypeRent(int typeRent) {
        TypeRent = typeRent;
    }

    public String getType() {
        return Type;
    }

    public void setType(String type) {
        Type = type;
    }

    @Override
    public int CalcRent(int days) {
        this.BrandRent(this);
        return this.TypeRent*days;
    }

    //根据车的型号和数量
    public Car(String type,int no) {
        Type = type;
        super.setNo(no);

    }
    //定义一个方法用于计算租车的数量*车型日租费
    public void BrandRent(Car car){
        int DailyRental;
        switch (car.getType()){
            case "别克商务舱GL8":
                DailyRental = 600;
                break;
            case "宝马550i":
                DailyRental = 500;
                break;
            case "别克林荫大道":
                DailyRental = 300;
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + car.getType());
        }
        car.setTypeRent(super.getNo()*DailyRental);
    }
}

Bus.java

 

package com.sbx.ex5;

/**
 * Created by BlueLover
 * Name:苏半夏
 * Date:2021/2/7
 * Time:16:39
 */
public final class Bus extends MotoVehicle{
    private int SeatCount;  //乘载数量
    private int TypeRent;//租车的数量*车型日租费

    @Override
    public int CalcRent(int days) {
        this.BrandRent(this);
        return this.TypeRent*days;
    }

    public int getSeatCount() {
        return SeatCount;
    }

    public void setSeatCount(int seatCount) {
        SeatCount = seatCount;
    }

    public int getTypeRent() {
        return TypeRent;
    }

    public void setTypeRent(int typeRent) {
        TypeRent = typeRent;
    }

    public Bus(int seatCount,int no) {
        SeatCount = seatCount;
        super.setNo(no);
    }
    //定义一个方法用于计算租车的数量*车型日租费
    public void BrandRent(Bus bus){
        int DailyRental;
        if (bus.getSeatCount()>16){
            DailyRental = 1500;
        }else{
            DailyRental = 800;
        }
        bus.setTypeRent(super.getNo()*DailyRental);
    }
}

Truck.java

package com.sbx.ex5;

/**
 * Created by BlueLover
 * Name:苏半夏
 * Date:2021/2/7
 * Time:18:53
 */
public class Truck extends MotoVehicle{
    private final int ton;//
    @Override
    public int CalcRent(int days) {
        return ton*50*days*super.getNo();
    }

    public Truck(int ton,int no) {
        this.ton = ton;
        super.setNo(no);
    }
}

Test.java

package com.sbx.ex5;

/**
 * Created by BlueLover
 * Name:苏半夏
 * Date:2021/2/7
 * Time:18:37
 */
public class Test {
    public static void main(String[] args) {
        /*
        * 2辆宝马
          1辆别克商务舱
          1辆金龙(34)座
          租5天共多少租金
        */
        //2辆宝马的租金
        Car car1 = new Car("宝马550i",2);
        car1.CalcRent(5);
        //1辆别克商务舱的租金
        Car car2 = new Car("别克商务舱GL8",1);
        car2.CalcRent(5);
        //1辆金龙(34)座的租金
        Bus bus = new Bus(34,1);
        bus.CalcRent(5);
        int sum = car1.CalcRent(5)+car2.CalcRent(5)+bus.CalcRent(5);
        System.out.println("2辆宝马,1辆别克商务舱,1辆金龙(34)座,租5天共需要租金"+sum+"元");

        Truck truck = new Truck(100,2);
        System.out.println("2辆卡车各100吨,租5天共需租金"+truck.CalcRent(5)+"元");
    }
}

 

Java练习——抽象类

原文:https://www.cnblogs.com/Blcjme/p/14386395.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!