码迷,mamicode.com
首页 > 编程语言 > 详细

5.02 java例题 汽油检测

时间:2018-05-04 11:57:53      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:int   result   java   3.5   class   get   span   ring   汽车   

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();
        
                

    }

}

 

5.02 java例题 汽油检测

标签:int   result   java   3.5   class   get   span   ring   汽车   

原文地址:https://www.cnblogs.com/F9801/p/8989382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!