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

本文介绍了一个简单的车辆租赁系统设计,包括抽象基类机动车类及继承的小轿车类和客车类,通过不同类型和座位数计算租金,并提供了主方法进行交互式选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

文件一,机动车类:

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);
    }
}

 

转载于:https://www.cnblogs.com/lwj820876312/p/7243126.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值