zoukankan      html  css  js  c++  java
  • 模拟电梯

    /**
     * 电梯类
     * @author H2
     *
     */
    public class Elevator {
        public int currentFloor=1;//当前楼层
        public int destinationFloor;//将要前往的楼层
        public boolean up;//上行或下降
        public boolean doorsOpen;//电梯关门或开门
        
        public Elevator(int currentFloor){
            this.currentFloor=currentFloor;
            System.out.println("这是一个有参构造方法");
        }
        
        public Elevator(){
            this(1);
            System.out.println("这是一个无参构造方法");
        }
        //开门
        public void openDoors(){
            doorsOpen=true;
            System.out.println("电梯正在开门......");
        }
        
        //关门
        public void closeDoors(){
            doorsOpen=false;
            System.out.println("电梯正在关门.......");
        }
        
        //电梯上行
        public void goingUp(){
            up=true;
            System.out.println("电梯正在上行.......");
        }
        
        //电梯下降
        public void goingDown(){
            up=false;
            System.out.println("电梯正在下降.......");
        }

        public void goToFloor(int floor){
            System.out.println("将要去"+floor+"层");
            if(floor>currentFloor){
                //上升
                goingUp();
                currentFloor=floor;
                System.out.println("电梯当前位置:"+currentFloor);
            }else if(floor<currentFloor){
                //下降
                goingDown();
                currentFloor=floor;
                System.out.println("电梯当前位置:"+currentFloor);
            }else if(floor==currentFloor){
                System.out.println("当前楼层就是目标楼层");
            }
        }
    }

    public static void main(String[] args) {
            System.out.println("===============================");
            Elevator elevator1=new Elevator();
            elevator1.goToFloor(100);
            Elevator elevator2=new Elevator(73);
            elevator2.goToFloor(1);
            /*elevator1.goToFloor(40);
            elevator1.goToFloor(10);
            elevator1.goToFloor(14);
            elevator1.goToFloor(50);
            elevator1.goToFloor(100);
            elevator1.goToFloor(4);
            elevator1.goToFloor(1);
            elevator1.goToFloor(20);
            elevator1.goToFloor(14);*/
            
            
            
            
            System.out.println("===============================");
            
        }

  • 相关阅读:
    软件杯第一阶段
    架构漫谈读后感二
    架构漫谈读后感一
    软件架构实践读后感三
    Refined Architecture阶段读后感
    软件架构实践读后感二
    Excel打开csv文件乱码
    安装机器学习所需的库
    认识Numpy Ndarray对象
    初识pandas
  • 原文地址:https://www.cnblogs.com/lvlei/p/4883965.html
Copyright © 2011-2022 走看看