- package woxihuan;
- public class Car {
- // 汽车的油量监控系统
- // 定义一个类 Car
- // Car: 品牌, 平均油耗, 邮箱容量
- // 什么牌子的车行驶了多少公里, 消耗汽油多少 L, 邮箱还剩多少 L
- //get
- public Car(String _name, Double _sal, int _volume, Double _distance) {
- name = _name;
- sal = _sal;
- volume = _volume;
- distance = _distance;
- operation = sal * distance;
- operation1 = volume - operation;
- }
- //set
- public Car() {
- name = "奥迪";
- sal = 13.5;
- volume = 90;
- distance = 1.5;
- operation = sal * distance;
- operation1 = volume - operation;
- }
- private String name; // 品牌
- private double sal;// 油耗
- private int volume;// 容量
- private double distance;// 行驶了多少公里
- private double operation;// 运算消耗汽油
- private double operation1;// 运算油箱还剩多少
- public void result() {
- System.out.println(name + "行驶了" + distance + "km, 消耗汽油:" + operation + "L, 油箱还剩:" + operation1 + "L." );
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- package woxihuan;
- public class Che {
- public static void main(String[] args) {
- //get
- Car e = new Car("奥迪 a3" ,8.2 , 55 ,2.6);
- e.result();
- //set
- Car e2 = new Car();
- e2.setName("奥迪 r8");
- e2.result();
- }
- }
来源: http://www.bubuko.com/infodetail-2586705.html