light.java
来自「用java模拟的电梯程序,可以选择有几层楼,一共有几个人在哪几层等电梯,可以不断」· Java 代码 · 共 97 行
JAVA
97 行
// Light.java// Light turns a light on or offpackage com.Anance.elevator.model;// Deitel packagesimport com.Anance.elevator.event.*;public class Light implements ElevatorMoveListener { // Light state (on/off) private boolean lightOn; // time before Light turns off automatically (3 seconds) public static final int AUTOMATIC_TURNOFF_DELAY = 3000; // LightListener listens for when Light should turn on/off private LightListener lightListener; // location where Light turned on or off private Location lightLocation; // set LightListener public void setLightListener( LightListener listener ) { lightListener = listener; } // turn on Light public void turnOnLight( Location location ) { if ( !lightOn ) { lightOn = true; // send LightEvent to LightListener lightListener.lightTurnedOn( new LightEvent( this, location ) ); lightLocation = location; // declare Thread that ensures automatic Light turn off Thread thread = new Thread( new Runnable() { public void run() { // turn off Light if on for more than 3 seconds try { Thread.sleep( AUTOMATIC_TURNOFF_DELAY ); turnOffLight( lightLocation ); } // handle exception if interrupted catch ( InterruptedException exception ) { exception.printStackTrace(); } } } // end anonymous inner class ); thread.start(); } } // end method turnOnLight // turn off Light public void turnOffLight( Location location ) { if ( lightOn ) { lightOn = false; // send LightEvent to LightListener lightListener.lightTurnedOff( new LightEvent( this, location ) ); } } // end method turnOffLight // return whether Light is on or off public boolean isLightOn() { return lightOn; } // invoked when Elevator has departed public void elevatorDeparted( ElevatorMoveEvent moveEvent ) { turnOffLight( moveEvent.getLocation() ); } // invoked when Elevator has arrived public void elevatorArrived( ElevatorMoveEvent moveEvent ) { turnOnLight( moveEvent.getLocation() ); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?