创建Car类

Posted beens

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建Car类相关的知识,希望对你有一定的参考价值。

技术分享
  1 package org.hanqi.pn0120;
  2 
  3 //汽车类
  4 public class Car {
  5     //颜色
  6     private String yanse;
  7     //品牌
  8     private String pinpai;
  9     //油箱容量
 10     private double rongliang=40;
 11     //当前油量
 12     private double dangqianyouliang=0;
 13     public double getDangqianyouliang()
 14     {
 15         return this.dangqianyouliang;
 16     }
 17     
 18     //加油
 19     public void jiayou(double youliang)
 20     {
 21         if(youliang<0)
 22         {
 23             System.out.println("不要偷油");
 24         }
 25         else if(this.dangqianyouliang+youliang>this.rongliang)
 26         {
 27             double benciyouliang=this.rongliang-this.dangqianyouliang;
 28             this.dangqianyouliang=this.rongliang;
 29             System.out.println("油箱已加满,这次加了"+benciyouliang+"升油");
 30         }
 31         else
 32         {
 33             this.dangqianyouliang+=youliang;
 34             System.out.println("这次加了"+youliang+"升油");
 35         }
 36         
 37     }
 38     private double xingshizonglucheng=0;
 39     public double getXingshizonglucheng()
 40     {
 41         return this.xingshizonglucheng;
 42     }
 43     //行驶
 44     public void xingshi(double xingshiluchen,double youhao)
 45     {
 46         if(this.dangqianyouliang<=0)
 47         {
 48             System.out.println("车没油了,怎么跑");
 49             return;
 50         }
 51         if(xingshiluchen<0||youhao<0)
 52         {
 53             System.out.println("不可能这么开车");
 54         }
 55         else
 56         {
 57         
 58         //油耗
 59         double dangqianyouhao=xingshiluchen*youhao/100;
 60         this.dangqianyouliang-=dangqianyouhao;
 61         
 62         //验证当前油量
 63         //第一种方式 三元运算法
 64         //this.dangqianyouliang=this.dangqianyouliang>0?this.dangqianyouliang:0;
 65         if(this.dangqianyouliang<0)
 66         {
 67             this.dangqianyouliang=0;
 68             System.out.println("你是推着车走的吗?");
 69         }
 70         
 71         this.xingshizonglucheng+=xingshiluchen;
 72         System.out.println("本次行驶了"+xingshiluchen+"公里");
 73         }
 74     }
 75     
 76     
 77     public String getYanse() {
 78         return yanse;
 79     }
 80     public void setYanse(String yanse) {
 81         this.yanse = yanse;
 82     }
 83     public String getPinpai() {
 84         return pinpai;
 85     }
 86     public void setPinpai(String pinpai) {
 87         this.pinpai = pinpai;
 88     }
 89     public double getRongliang() {
 90         return rongliang;
 91     }
 92     public void setRongliang(double rongliang) {
 93         this.rongliang = rongliang;
 94     }
 95     public Car(String pinpai, double rongliang) {
 96         super();
 97         this.pinpai = pinpai;
 98         this.rongliang = rongliang;
 99     }
100     
101     //main方法
102     public static void main(String[] args)
103     {
104         Car myBenchi=new Car("奔驰",50);
105         System.out.println("品牌="+myBenchi.getPinpai());
106         System.out.println("油箱容量="+myBenchi.getRongliang());
107         myBenchi.xingshi(200, 13);
108         myBenchi.jiayou(30);
109         myBenchi.jiayou(30);
110         System.out.println("当前油量="+myBenchi.getDangqianyouliang());
111         myBenchi.xingshi(300,15);
112         
113         
114         System.out.println("当前油量="+myBenchi.getDangqianyouliang());
115         myBenchi.xingshi(200, 13);
116         System.out.println("当前总里程="+myBenchi.xingshizonglucheng);
117         System.out.println("当前油量="+myBenchi.getDangqianyouliang());
118     }
119     
120     
121 }
View Code

技术分享

以上是关于创建Car类的主要内容,如果未能解决你的问题,请参考以下文章

创建多个具有相同名称的类对象? C++

JAVA种用一个类去调用一个接口的多个实现类

spring autowired怎么按照name注入

片段 null 必须是公共静态类才能从实例状态正确重新创建

JAVA程序 特急!!!!!!!!!!送分!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Python - 如何从打印信息的单独进程中实时读取类实例