阅读以下说明及Java程序,将应填入 (n) 处的字句写在对应栏内。
[说明]
传输门是传输系统中的重要装置。传输门具有Open(打开)、Closed(已关闭)、Opening(正在打开)、StayOpen(保持打开)、Closing(正在关闭)5种状态。触发状态的转换事件有crick、complete和timeout三种。事件与其相应的状态转换如图18-6所示。
下面的“Java代码1”与“Java代码2”分别用两种不同的设计思路对传输门进行状态模拟,请填补代码中的空缺。
[Java代码1]
pubZic class Door
public static final int CLOSED=1;public static final int OPENING=2;
public static final int OPEN=3;public static final int CLOSING=4;
public static final int STAYOPEN=5;private int stare=CLOSED;
//定义状态变量,用不同的整数表示不同状态
private void setState(int state) (this.stale=stare;
//设置传输门当前状态
public void getState()
//此处代码省略,本方法输出状态字符串
//例如,当前状态为CLOSED时,输出字符串为"CLOSED"
public void click()//发生click事件时进行状态转换
if( (1) )setState(OPENING);
else if( (2) )setStare(CLOSZNG);
else if( (3) )setStare(STAYOPEN);
//发生timeout事件时进行状态转换
public void timeout()(if(state==OPEN)setState(CLOSING);
pubnc void complete()//发生complete事件时进行状态转换
if(state==OPENING) setState(OPEN);
else if(state==CLOSING) setState(CLOSED);
public Static void main (String [] args)
Door aDoor=new Door();
aDoor.geLStaLe();aDoor.click();aDoor.getState();aDoor.complete();
aDoor.getState();aDoor.click();aDoor.getState();aDoor.clik();
aDoor.getState();return;
[Java代码2]
public class Door
public final DoorState CLOSED=new DoorClosed(this);
public final DoorState OPENING=new Dooropening(this);
public final DoorState OPEN=new DoorOpen(this);
public final DoorState CLOSING=new DoorClosing(this);
public final DoorState STAYOPEN=new DoorStayopen(this);
private DoorState state=CLOSED;
//设置传输门当前状态
public void setState(DoorState state)(this.state=state;
public void getState()//根据当前状态输出对应的状态字符串
System.out.printIn(state.getClass().getName());
public void click()( (4) ;//发生click事件时进行状态转换
public void timeout()( (5) ;//发生timeout事件时进行状态转换
public void complete()( (6) ;//发生complete事件时进行状态转换
public static void main(String[]args)
Door aDoor=new Door():
aDoor.getState();aDoor.Click();aDoor.getState();aDoor.complete();
aDoor.getstate();aDoor.timeout();aDoor.getState();return;
public abstract class DoorState//定义所有状态类的基类
protected Door door:
public DoorState(Door door)(this.door=door;
public void click()
public coid complete()
public void timeout()
class DoorClosed extends DoorState//定义一个基本的Closed状态
public DoorClosed(Door door)(super(door);
public void click() (7) ;
//该类定义的其余代码省略
//其余代码省略