1 package com.hanqi; 2 3 //汽车 4 public class Car { 5 6 // 车牌 7 private String CheP; 8 9 // 油箱容量 10 private double YouXRL = 40; 11 12 // 总里程数 13 private double ZongLC = 0; 14 15 // 油耗 16 private double YouH = 10; 17 18 // 当前油量 19 private double DangQYL = 0; 20 21 public String getCheP() { 22 return CheP; 23 } 24 25 public void setCheP(String cheP) { 26 CheP = cheP; 27 } 28 29 public double getYouXRL() { 30 return YouXRL; 31 } 32 33 public void setYouXRL(double youXRL) { 34 YouXRL = youXRL; 35 } 36 37 public double getZongLC() { 38 return ZongLC; 39 } 40 41 public void setZongLC(double zongLC) { 42 ZongLC = zongLC; 43 } 44 45 public double getYouH() { 46 return YouH; 47 } 48 49 public void setYouH(double youH) { 50 51 if(youH >0) 52 { 53 YouH = youH; 54 } 55 else 56 { 57 System.out.println("油耗数据错误"); 58 } 59 60 } 61 62 public double getDangQYL() { 63 return DangQYL; 64 } 65 66 public void setDangQYL(double dangQYL) { 67 DangQYL = dangQYL; 68 } 69 70 public Car(String cheP) { 71 super(); 72 CheP = cheP; 73 } 74 75 //方法 76 //加油 77 public void JiaYou(double youl) 78 { 79 if(youl < 0 ) 80 { 81 System.out.println("不要偷我的油"); 82 } 83 else if(youl > (YouXRL - DangQYL)) 84 { 85 DangQYL = YouXRL; 86 87 System.out.println("邮箱满了,装不下了"); 88 } 89 else 90 { 91 DangQYL += youl; 92 93 System.out.println("加油量 = " + youl); 94 } 95 } 96 //加油方法的重载,一次加满 97 public double JiaYou() 98 { 99 //计算 100 double rtn = YouXRL - DangQYL; 101 102 JiaYou(rtn); //便于代码维护 103 104 return rtn; 105 } 106 107 108 109 //行驶 110 public void XingShi (double licheng) 111 { 112 if(licheng < 0 ) 113 { 114 System.out.println("车不能这样开"); 115 } 116 else 117 { 118 //当前油量 119 DangQYL -= licheng * YouH /100; 120 121 //总里程 122 ZongLC += licheng; 123 } 124 125 } 126 127 128 129 130 131 132 133 134 135 }
1 package com.hanqi; 2 3 public class Test11car { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 8 Car car1 = new Car("鲁c12345"); 9 10 car1.setYouXRL(50); 11 12 System.out.println("油箱容量 = " + car1.getYouXRL() ); 13 14 car1.JiaYou(40); 15 16 System.out.println("当前油量 = " + car1.getDangQYL()); 17 18 car1.JiaYou(); 19 20 System.out.println("当前油量 = " + car1.getDangQYL() ); 21 22 23 car1.setYouH(7); 24 25 //行驶 26 27 car1.XingShi(100); 28 29 System.out.println("当前油量 = "+ car1.getDangQYL()); 30 System.out.println("总里程 = " + car1.getZongLC()); 31 32 33 34 35 36 37 } 38 39 }