类的继承

/*
某汽车租借公司有多种汽车可以出租,计算汽车的租金
Vehicle是所有车的父类,属性:品牌,车牌号
                       方法:返回总租金的方法:public double getSumRent(int days){}
Car:小轿车  属性:车型(两厢,三厢,越野)
            两厢:每天300  三厢:每天400  越野:每天500
Bus:多座位汽车   属性:座位数
                 8<座位数<=16:每天400
                 座位数>16:每天600
测试类:根据用户选择不同的车,计算总租金并输出
 */
package day_6;

public class Vehicle {
    private String brand;
    private String id;
    public Vehicle(){

    }
    public Vehicle(String brand,String id){
        this.brand=brand;
        this.id=id;
    }
    public double getSumRent(int days){
        return 0;
    }
}
package day_6;

public class Car extends Vehicle {
    private String type;
    public Car(){   //编写无参构造方法

    }
    public Car(String type){   //编写带参构造方法
        this.type=type;
    }
    public double getSumRent(int days){  //对父类的getSumRent方法进行重写
        switch (this.type){
            case "两厢":
                return 300*days;
            case "三厢":
                return 400*days;
            case "越野":
                return 500*days;
        }
        return 0;
    }

}
package day_6;

public class Bus extends Vehicle {
    private int seats;
    public Bus(){

    }
    public Bus(int seats){
        this.seats=seats;
    }
    public double getSumRent(int days){
        if(days<=16 && days>8){
            return 400*days;
        }
        else
            return 600*days;
    }
}
package day_6;
import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入小轿车车的型号和租的天数:");
        Car car = new Car(input.next());
        System.out.println("总租金是:"+ car.getSumRent(input.nextInt()));
        System.out.println("******************************************");
        System.out.print("请输入多座位车的座位数和租的天数:");
        Bus bus=new Bus(input.nextInt());
        System.out.println("总租金是:"+ bus.getSumRent(input.nextInt()));
    }
}

  

转载于:https://www.cnblogs.com/-slz-2/p/11255174.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值