com.sun.test.innerclass Package; Import of java.util.ArrayList; Import java.util.List; / ** * the Created by Panda ON 2018/6/11. * / public class the Controller { Private List <the Event> = EventList the ArrayList new new <> (); public void addEvent (the Event Event) {eventList.add (Event);} public void RUN () { the while (eventList.size ()> 0) { for (the Event Event: new new the ArrayList <> ( eventList)) {// here only with new ArrayList <> (eventList) thus converted into a list otherwise remove the abnormal because the iterator has limitations System.out.println (new ArrayList <> (eventList )); // output here the actual content is EventList IF (event.ready ()) { System.out.println (Event); event.action (); EventList.remove(event); } } } } }
package com.sun.test.innerclass; /** * Created by Panda on 2018/6/11. */ public abstract class Event { private long eventTime; protected final long delayTime; public Event(long delayTime){ this.delayTime=delayTime; start(); } public void start(){ eventTime=System.nanoTime()+delayTime; } public boolean ready(){ return System.nanoTime()>=eventTime; } public abstract void action(); }
package com.sun.test.innerclass; /** * Created by Panda on 2018/6/11. */ public class GreenhouseController { /** * 测试内部类 的作用 * * @author JiangYuan Sun * @date 2019-04-26 10:59 */ public static void main(String[] args) { GreenhouseControls greenhouseControls = new GreenhouseControls(); greenhouseControls.addEvent(greenhouseControls.new Bell(900)); Event[] events={greenhouseControls.new ThermostatNight(0),greenhouseControls.new LightOn(200), greenhouseControls.new LightOff(400),greenhouseControls.new WaterOn(600), greenhouseControls.new WaterOff(800),greenhouseControls.new ThermostatDay(1400)}; greenhouseControls.addEvent(greenhouseControls.new Restart(2000,events)); if(args.length==1){ greenhouseControls.addEvent(new GreenhouseControls.Terminate(new Integer(args[0]))); } greenhouseControls.run(); } }
package com.sun.test.innerclass; /** * Created by Panda on 2018/6/11. */ public class GreenhouseControls extends Controller { private boolean light=false; public class LightOn extends Event{ public LightOn(long delayTime){super(delayTime);} @Override public void action() { light=true; } public String toString(){return "Light is on";} } public class LightOff extends Event{ public LightOff(long delayTime){super(delayTime);} @Override public void action() { light=false; } public String toString(){return "Light is off";} } private boolean water=false; public class WaterOn extends Event{ public WaterOn(long delayTime){super(delayTime);} @Override public void action() { water=true; } public String toString(){return "Greenhouse water is on";} } public class WaterOff extends Event{ public WaterOff(long delayTime){super(delayTime);} @Override public void action() { water=false; } public String toString(){return "Greenhouse water is off";} } private String thermostat="Day"; public class ThermostatNight extends Event{ public ThermostatNight(long delayTime){super(delayTime);} @Override public void action() { thermostat="Night"; } public String toString(){return "Thermostat on night setting";} } public class ThermostatDay extends Event{ public ThermostatDay(long delayTime){super(delayTime);} @Override public void action() { thermostat="day"; } public String toString(){ return "Thermostat on day setting"; } } public class Bell extends Event{ public Bell(long delayTime){super(delayTime);} @Override public void action() { addEvent(new Bell(delayTime)); } public String toString(){return "Bing";} } public class Restart extends Event{ private Event[] eventList; public Restart(long delayTime,Event[] eventList){ super(delayTime); this.eventList=eventList; for(Event event:eventList) addEvent(event); } @Override public void action() { for(Event event:eventList){ event.start(); addEvent(event); } start(); addEvent(this); } public String toString(){return "Restarting system";} } public static class Terminate extends Event{ public Terminate(long delayTime){super(delayTime);} @Override public void action() { System.exit(0); } public String toString(){ return "Terminating"; } } }