12.10课堂学习----实例化、构造方法案例

package com.hanqi;

public class Car
  {
    //品牌
    private String pinP;
    
    //
    public String getPinP() 
    {
        return pinP;
    }

    //
    public void setPinP(String pinP) 
    {
        System.out.println("车的品牌设置为" + pinP);
        
        this.pinP = pinP;
    }
    
    //状态
    private String ZhuangT = "静止";
    
    public String getZhuangT() 
    {
        return ZhuangT;
    }
    
    //油量
    private double youL = 0;
    

    public double getYouL() 
    {
        return youL;
    }
    
    //油箱
    private double youX;

    public double getYouX() 
    {
        return youX;
    }

    public void setYouX(double youX) 
    {
        this.youX = youX;
    }
    
    //一次加满   方法的重载
    public void jiaYou()
    {
        System.out.println("一次加满");
        
        double yjyl = this.youX - this.youL;
        
        jiaYou(yjyl);
    }

    //加油
    public void jiaYou(double youl)
    {
        //油箱容量大于0
        if (youX <= 0)
        {
            System.out.println("忘装油箱了");
            
            return;
        }

        //加油量大于0
        else if (youl < 0)
        {
            System.out.println("不要偷我的油");
            
            return;
        }
        //只有静止状态下才能加
        else if (!ZhuangT.equals("静止"))
        {
            System.out.println("车熄火了再加油");
            
            return;
        }
            //不能超过油箱容量
        else if (this.youL + youl > this.youX)
        {
            System.out.println("油箱已经加满了,不要再加了");
            
            double syyl = this.youL;
            
            this.youL = this.youX;
            
            System.out.println("这次加了" + (this.youX - syyl) + "升油");
        }
        //剩余油量不能小于0
        else
        {
        this.youL += youl;
        
        System.out.println("这次加了" + youl + "升油");
        }
    }
    
    public void faDong()//无参数也无返回值的方法
    {
        if (this.youL<=0)
        {
            System.out.println("没油了,请先加油");
        }
        else if(!this.ZhuangT.equals("静止"))
        {
            System.out.println("车已经发动了");
        }
        else{
            this.ZhuangT = "发动";
            System.out.println("车发动了");
        }
    }
    private double zongLC;
    
    public double getZongLC() {
        return zongLC;
    }

    //行驶
    public void xingS(double lic,double youh)//lic里程youh油耗
    {
        //车的状态是发动
        if(!this.ZhuangT.equals("发动"))
        {
            System.out.println("请发动汽车");    
        }
        else
        {
            //行驶
            //double zyh = lic * youh / 100;
            
            double lc = this.youL * 100 / youh;
            if(lc<lic)
            {
                System.out.println("最多行驶"+lc+"公里");
                this.zongLC +=lc;
                this.youL = 0;
                
            }
            else
            {
                System.out.println("行驶了"+ lic +"公里");
                this.zongLC += lic;
                this.youL -= lic * youh / 100;
                
            }
            this.ZhuangT = "静止";
        }
        
        //计算总油耗,判断是否没有油了
    }


    /*public Car()
    {
        System.out.println("调用了构造方法");
    }*/
    
    //重载
    public Car(String pinP)
    {
        System.out.println("实例化车的品牌为"+pinP);
        this.pinP=pinP;
    }
    
    public static void main(String[] args) {


        Car mycar = new Car("宝马");//默认构造方法
        
        //mycar.setPinP("宝马");
        mycar.xingS(100, 10);
        
        
        
        mycar.setYouX(40);
        
        mycar.jiaYou(20);
        
        System.out.println("车的品牌是"+mycar.pinP+",车的油量是" + mycar.getYouL() + "升,车的状态是" + mycar.getZhuangT());

        
        mycar.faDong();
        mycar.xingS(300, 10);

        mycar.jiaYou(10);
        
        System.out.println("车的油量是" + mycar.getYouL() + "升,车的状态是" + mycar.getZhuangT());

        

        mycar.jiaYou();
        
        System.out.println("车的油量是" + mycar.getYouL() + "升,车的状态是" + mycar.getZhuangT());

    }
}

测试结果:

原文地址:https://www.cnblogs.com/0927wyj/p/5037187.html