为了账号安全,请及时绑定邮箱和手机立即绑定

租车系统 封装 继承 多态

父类Car

package rentCar;

public class Car {
    //封装思想  1.属性私有化  2.生成set/get方法
    private String carName;
    private int price;
    private int personLoadnum;
    private int goodsLoadnum;
    public String getCarName() {
        return carName;
    }
    public void setCarName(String carName) {
        this.carName = carName;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public int getPersonLoadnum() {
        return personLoadnum;
    }
    public void setPersonLoadnum(int personLoadnum) {
        this.personLoadnum = personLoadnum;
    }
    public int getGoodsLoadnum() {
        return goodsLoadnum;
    }
    public void setGoodsLoadnum(int goodsLoadnum) {
        this.goodsLoadnum = goodsLoadnum;
    }

}

两个子类 载人(personCar) 载物(goodsCar) 皮卡内容类似 所以没有敲

//personCar
package rentCar;

public class personCar extends Car {
    public personCar(String carname,int price,int personnum){
//构造方法,用于之后创建实例
        this.setCarName(carname);
        this.setPrice(price);
        this.setPersonLoadnum(personnum);
    }
    @Override
    public String toString() {
        return this.getCarName()+"\t"+this.getPrice()+"元/天    载人  "+this.getPersonLoadnum();
    }
}
//goodsCar
package rentCar;

public class goodsCar extends Car {
    public goodsCar(String carname,int price,int goodsnum){
        this.setCarName(carname);
        this.setPrice(price);
        this.setGoodsLoadnum(goodsnum);
    }
    @Override
    public String toString() {
        return this.getCarName()+"\t"+this.getPrice()+"元/天    载物"+this.getGoodsLoadnum()+"吨";
    }
}

测试类User

package rentCar;

import java.util.Scanner;

public class User {

    public static void main(String[] args) {
    //这里是 "引用多态"的思想 父类引用指向子类对象
            Car [] cars={  
                new personCar("雪佛兰",200,4),
                new personCar("BMW",500,2),
                new personCar("奥迪",300,3),
                new goodsCar("长江一号",800,10),
                new goodsCar("长江二号",600,8),
                new goodsCar("长江三号",1600,24)
                };
        System.out.println("-----租车系统启用-------");
        System.out.println();
        System.out.println("-----输入1 开始租车-输入0退出租车------");
        Scanner input=new Scanner(System.in);
        if(input.nextInt()==1){
            System.out.println("---以下为本公司的租车信息----");
            System.out.println();
            for(int i=0;i<cars.length;i++){
                System.out.println("No."+(i+1)+"\t"+cars[i]);//打印租车列表信息 方便选择序号
            }
        }
        else{
            System.out.println("---欢迎下次光临--");
        }
        System.out.println("-----输入租车数量--------");
        int num=input.nextInt();
        int priceSum = 0,personSum=0,goodsSum=0;
        for(int i=0;i<num;i++){
            System.out.println("需要租的第"+(i+1)+"辆车的序号");
            int order= input.nextInt();
            priceSum=priceSum+cars[order-1].getPrice();//计算总价 下同
            personSum=personSum+cars[order-1].getPersonLoadnum();
            goodsSum=goodsSum+cars[order-1].getGoodsLoadnum();
        }
        System.out.println("租车成功");
        System.out.println("总价:"+priceSum+"元/天"+"  "+"可载人"+personSum+"  "+"可载物"+goodsSum+"吨");
        input.close();
    }

}
点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消