zoukankan      html  css  js  c++  java
  • 重载

    package duixiang;
    
    public class Car {
        // 品牌
        private String pinpai;
    
        //
        public String getPinpai() {
            return pinpai;
        }
    
        //
        public void setPinpai(String pinpai) {
            System.out.println("车的品牌设置为" + pinpai);
            this.pinpai = pinpai;
        }
    
        // 状态
        private String zhuangtai = "静止";
    
        public String getzhuangtai() {
            return zhuangtai;
        }
    
        // 油量
        private double youliang = 0;
    
        public double getYouliang() {
            return youliang;
        }
    
        // 油箱
        private double youxiang;
    
        public double getYouxiang() {
            return youxiang;
        }
    
        public void setYouxiang(double youxiang) {
            this.youxiang = youxiang;
        }
    
        // 一次加满
        public void jiayou() {
            System.out.println("一次加满");
            double yici = this.youxiang - this.youliang;
            jiayou(yici);
        }
    
        // 加油
        public void jiayou(double youliang) {
            // 油箱容量大于0
            if (youxiang <= 0) {
                System.out.println("你忘了带油箱");
                return;
            }
            // 加油量大于0
            else if (youliang < 0) {
                System.out.println("不要偷我的油");
                return;
    
            }
            // 静止状态才能加油
            else if (!zhuangtai.equals("静止")) {
                System.out.println("车停了再加油");
                return;
            }
            // 不能超过油箱容量
            else if (this.youliang + youliang > this.youxiang) {
                System.out.println("油箱已将加满了,不要再加了");
    
                double shengyu = this.youliang;
                this.youliang = this.youxiang;
                System.out.println("这次加了" + (this.youxiang - shengyu) + "升油");
            } else {
                this.youliang += youliang;
            }
            System.out.println("这次加了" + youliang + "升油");
        }
    
        //发动
        public void fadong() {
            if (this.getYouliang() <= 0) {
                System.out.println("请加油");
            } else if (!this.getzhuangtai().equals("静止")) {
                System.out.println("已经发动");
            } else {
                this.zhuangtai = "发动";
                System.out.println("车子发动了");
            }
        }
        
        private double zongli;
        
        public double getZongli() {
            return zongli;
        }
    
        //行驶
        public void xingshi(double lu,double youhao)
        {
            //车的状态是发动
            if (!this.zhuangtai.equals("发动"))
            {
                System.out.println("请发动汽车");
            }
            else
            {
                //行驶
                //double zongli=lu*youhao/100;
                double lc=this.youliang*100/youhao;
                if(lc<lu)
                {
                    System.out.println("最多行驶"+lc+"公里");
                    this.zongli+=lc;
                    
                    this.youliang=0;
                    this.zhuangtai="静止";
                }
                else
                {
                    System.out.println("行驶了"+lu+"公里");
                    this.zongli+=lu;
                    
                    this.youliang-=lu*youhao/100;
                    
                }
                this.zhuangtai="静止";
            }
            
            //计算总油耗,判断油量
        }
    
        public Car() {
            System.out.println("默认的构造方法");
        }
    
        // 重载
        public Car(String pinpai) {
            System.out.println("实例化车的品牌为" + pinpai);
            this.pinpai = pinpai;
        }
    
    }
    package duixiang;
    
    public class Test3 {
    
        public static void main(String[] args) {
            Car car= new Car();//默认构造方法
            car.setPinpai("宝马");
            car.xingshi(100, 10);
            car.fadong();
            System.out.println("车的品牌是"+car.getPinpai()+"车的油量是"+car.getYouliang()+"车的状态是"+car.getzhuangtai());
            car.setYouxiang(40);
            car.jiayou(10);
            car.fadong();
            car.xingshi(200, 10);
            car.jiayou(20);
            car.jiayou(10);
            System.out.println("车的品牌是"+car.getPinpai()+"车的油量是"+car.getYouliang()+"车的状态是"+car.getzhuangtai());
    
        }
    
    }
  • 相关阅读:
    python数据库小脚本
    替换debug
    监听属性变化
    ast binding
    还原未修改的常量
    数据库中常用查询
    职业生涯四个阶段
    如何控制需求
    如何将VS Code 切换成中文
    当同时使用bootstrapdatepicker.js和jquery.validate.js这两款插件,至少要选择两次时间,才能验证成功的问题
  • 原文地址:https://www.cnblogs.com/doubiqi/p/5044125.html
Copyright © 2011-2022 走看看