汽车
package 面向对象编程; //汽车 public class Car { //颜色 private String yanse; //品牌 private String pinpai="奔驰"; //油箱容量 private double youxiangrl=40; //当前油量 private double dangqianyl=0; public Car(String string, int i) { // TODO 自动生成的构造函数存根 } public double getDangqianyl() { return this.dangqianyl; } //加油 public void jiaYou(double youliang) { if(youliang<0) { System.out.println("不要偷油"); } else if (this.dangqianyl+youliang>this.youxiangrl) { double bcyl=this.youxiangrl-this.dangqianyl; this.dangqianyl=this.youxiangrl; System.out.println("油太多了,已经加满"); } else { this.dangqianyl+=youliang; System.out.println("这次加了"+youliang+"升油"); } this.dangqianyl+=youliang; } private double xingshizlc=0; private double getXingshizlc() { return this.xingshizlc; } //行驶 public void xingshi(double xingshilc,double youhao) { if(xingshilc<0||youhao<0) { System.out.println("不可能这么开车"); } //油耗 double dangqianyh=xingshilc*youhao/100; this.dangqianyl-=dangqianyh; this.xingshizlc+=xingshilc; } public String getYanse() { return yanse; } public void setYanse(String yanse) { this.yanse = yanse; } public String getPinpai() { return pinpai; } public void setPinpai(String pinpai) { this.pinpai = pinpai; } public double getYouxiangrl() { return youxiangrl; } public void setYouxiangrl(double youxiangrl) { this.youxiangrl = youxiangrl; } //main方法 public static void main(String[]args) { Car myBenchi=new Car("奔驰",50); System.out.println("品牌="+myBenchi.getPinpai()); System.out.println("油箱容量="+myBenchi.getYouxiangrl()); myBenchi.jiaYou(30); myBenchi.jiaYou(30); System.out.println("当前容量="+myBenchi.getDangqianyl()); myBenchi.xingshi(300, 15); System.out.println("当前容量="+myBenchi.getDangqianyl()); } }