日期:2014-05-20 浏览次数:20719 次
package innerclasses.controller;
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 innerclasses.controller;
import java.util.*;
public class Controller {
private List<Event> eventList=new LinkedList<Event>();
public void addEvent(Event c){eventList.add(c);}
/*public void run(){
Iterator<Event> it=eventList.iterator();
List<Event> delList=new LinkedList<Event>();
while(eventList.size()>0){
it=eventList.iterator();
while(it.hasNext()){
Event e=it.next();
if(e.ready()){
System.out.println(e);
e.action();
//delList.add(e);
}
}
}
}*/
/*public void run(){
Iterator<Event> it=eventList.iterator();
//Event e=it.next();
//e.action();
while(eventList.size()>0){
it=eventList.iterator();
while(it.hasNext()){
Event e=it.next();
if(e.ready()){
System.out.print(e);
//e.action();
}
}
}
}*/
public void run(){
Iterator<Event> it=eventList.iterator();
List<Event> delList=new LinkedList<Event>();
while(eventList.size()>0){
it=eventList.iterator();
/*for(Event e:new ArrayList<Event>(eventList))
if(e.ready()){
System.out.println(e);
e.action();
eventList.remove(e);
}*/
while(it.hasNext())//尼玛找不出来是什么问题啊
{
Event e=it.next();//迭代器的next能否直接给对象赋值
if(e.ready()){
System.out.println(e);
e.action(); //action注释后运行无碍
//eventList.remove(e);
delList.add(e);
}
}
eventList.removeAll(delList);
}
}
}
package com.leo.lesson10.Test19;
import innerclasses.controller.*;
public class GreenhouseControls extends Controller{
private boolean isblow=false;
public class BlowerOn extends Event{
public BlowerOn(long delayTime){super(delayTime);}
public void action(){
isblow=true;
}
public String toString(){return "Blower is on";}
}
public class BlowerOff extends Event{
public BlowerOff(long delayTime){super(delayTime);}
public void action(){
isblow=false;
}
public String toString(){return "Blower is off";}
}
private boolean light=false;
public class LightOn extends Event {
public LightOn(long delayTime){ super(delayTime);}
public void action(){
light=true;
}
public String toString() {return "Light is on";}
}
public class LightOff extends Event {
public LightOff(long delayTime){super(delayTime);}
public void action(){
light=false;
}