本帖最后由 刘芮铭 于 2012-8-28 11:31 编辑
public enum Lamp {
S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false), //张老说这里12条路线我们只考虑4天路线,因为有几条是相对应的!这里可以理解!
S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
private Lamp(String opposite,String next,boolean lighted){
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
private boolean lighted;
private String opposite;
private String next;
public boolean isLighted(){
return lighted;
}
public void light(){
this.lighted = true;
if(opposite != null){ //这里说如果相对的灯不为空的话,在自己变绿的情况下也将对面的灯变绿!但是前面N2S(null,null,false)等4个把相对应的灯和下一个灯忽略了,那
Lamp.valueOf(opposite).light(); //岂不是自己变绿了后,对应的灯却不能变绿?
}
System.out.println(name() + " lamp is green,下面总共应该有6个方向能看到汽车穿过!");
}
public Lamp blackOut(){
this.lighted = false;
if(opposite != null){ //与上述问题一样
Lamp.valueOf(opposite).blackOut();
}
Lamp nextLamp= null;
if(next != null){
nextLamp = Lamp.valueOf(next);
System.out.println("绿灯从" + name() + "-------->切换为" + next);
nextLamp.light();
}
return nextLamp;
}
}
这个确实有点绕弯,看了2遍才看懂!请各位大侠赐教!
|
|