写过这个项目的人代码都有。下面这段是关灯的一个方法
public Lamp blackOut() {
lighted = false;
System.out.println(this+" 的红灯亮了");
if (oppositeLighted != null) {
Lamp.valueOf(oppositeLighted).blackOut();
}
Lamp nextLamp = null;
if(nextLight!=null){
nextLamp = Lamp.valueOf(nextLight);
nextLamp.light();
}
return nextLamp;
}
public class LampController {
private Lamp controlLamp= null;
public LampController(){
controlLamp = Lamp.S2N;
controlLamp.light();
ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate(
new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
controlLamp = controlLamp.blackOut(); }
},
10,
10,
TimeUnit.SECONDS);
}
}
想了很久都不太明白我用红色标记的那个地方,为什么要用个对象来接收。能把逻辑详细的讲解一下吗? |