一个面向对象的继承,抽象,私有属性和重写方法的综合实例

 //要求,按照车子类型天数计算租金

文件一,机动车类:

public abstract class Moto {
    public String nm;
    public String color;
    public String brand;
    public String mil;

    /**
     * 计算租金
     *
     * @param days
     * @return
     */
    public abstract int Calrentint(int days);
}

 

文件二,小轿车类:

public class Car extends Moto {
    public final static int Type1 = 1;
    public final static int Type2 = 2;
    public final static int Type3 = 3;

    private int type;

    public int getType() {
        return type;
    }

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

    @Override
    /**
     * 按照类型计算租金
     */
    public int Calrentint(int days) {
        int money = 0;
        switch (this.type) {
            case Type1:
                money = 600 * days;
                break;
            case Type2:
                money = 400 * days;
                break;
            case Type3:
                money = 300 * days;
                break;
        }
        return money;
    }
}

 

文件三,客车类:

public class Bus extends Moto {
    public int seatCount;

    public int getSeatCount() {
        return seatCount;
    }

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

    @Override
    /**
     * 按照座位数计算租金
     */
    public int Calrentint(int days) {
        int money = 0;
        if (this.seatCount <= 16) {
            money = days * 800;
        } else if (this.seatCount > 16) {
            money = days * 1600;
        }
        return money;
    }
}

 

文件四,主方法:

import java.util.Scanner;
public class Ui {
    public static void main(String[] args) {
        int money = 0;
        System.out.println("请选择租车类型");
        Scanner input = new Scanner(System.in);
        System.out.println("请选择1,出租车2.客车");
        int chose = input.nextInt();
        switch (chose) {
            case 1:
                System.out.println("请选择出租车类型(1.别克商务舱GL8 2.宝马550i 3.别克林荫大道)");
                int t = input.nextInt();
                System.out.println("输入租车天数");
                int d = input.nextInt();
                Car car = new Car();
                car.setType(t);
                money = car.Calrentint(d);
                break;
            case 2:
                System.out.println("请选择座位数");
                int bt = input.nextInt();
                System.out.println("输入租车天数");
                int bd = input.nextInt();
                Bus bus = new Bus();
                bus.setSeatCount(bt);
                money = bus.Calrentint(bd);
                break;
        }
        System.out.println("需要租金" + money);
    }
}

 

posted @ 2017-07-27 09:37  斯丢皮德曼  阅读(307)  评论(0编辑  收藏  举报