package com.isoftstone.interview.traffic;
public enum Lamp
{
//(对应的灯,下一个变绿的灯,我的状态)
//北N 南S 西W 东E
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),
S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
private String opposite;//对应的灯
private String next;//下一个变绿的灯
private boolean lighted;//我的状态
private Lamp(String opposite,String next,boolean lighted)
{
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
public boolean isLighted()
{
return lighted;
}
public void light()
{
this.lighted = true;
if(opposite != null)
{
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;
}
} |