软件设计师案例分析当天每日一练试题地址:www.cnitpm.com/exam/ExamDayAL.aspx?t1=4
往期软件设计师每日一练试题汇总:www.cnitpm.com/class/27/e4_1.html
软件设计师案例分析每日一练试题(2024/11/27)在线测试:www.cnitpm.com/exam/ExamDayAL.aspx?t1=4&day=2024/11/27
点击查看:更多软件设计师习题与指导
软件设计师案例分析每日一练试题内容(2024/11/27)
试题七
阅读以下说明以及Java程序。
【说明】
传输门是传输系统中的重要装置。传输门具有Open(打开)、Closed(关闭)、Opening (正在打开)、StayOpen(保持打开)和Closing(正在关闭)五种状态。触发状态的转换事件有click、complete和timeout三种。事件与其相应的状态转换如下图所示。
下面的Java代码1与Java代码2分别用两种不同的设计思路对传输门进行状态模拟,请填补代码中的空缺。
【Java代码1】
public 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 state = CLOSED;
//定义状态变量,用不同的整数表示不同状态
private void setState(int state){ this.state = state;}
//设置传输门当前状态
public void getState() {
//此处代码省略,本方法输出状态字符串,
//例如,当前状态为CLOSED时,输出字符串为"CLOSED"
}
public void click() { //发生click事件时进行状态转换
if ( (1) ;) setState(OPENING.;
else if ( (2) ;) setState{CLOSING.;
else if ( (3) ;) setState(STAYOPEN);
}
//发生timeout事件时进行状态转换
public void timeout() { if (state == OPEN) setState(CLOSING.;}
public 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.getState();aDoor.click();aDoor.getState();aDoor.complete();
aDoor.getState();aDoor.click();aDoor.getState();aDoor.click();
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.println(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 doer) { this.door = door;}
public void click() {}
public void complete() {}
public void timeout() {}
}
class DoorClosed extends DoorState { //定义一个基本的Closed状态
public DoorClosed(Door door) { super(door);}
public void click() { (7) ;)
//该类定义的其余代码省略
}
//其余代码省略
信管网考友试题答案分享:
信管网cnit**************:
getstate()==“opening”<br>getstate()==“closing”<br>getstate()==“stayopen”<br>
信管网cnit**************:
1 state==closing || state==xlosed<br>2 state==stay open || state==opening<br>3 state==open<br>4 state.click()<br>5 state.tineout()<br>6 state.complete()<br>7 <br>
信管网订阅号
信管网视频号
信管网抖音号
温馨提示:因考试政策、内容不断变化与调整,信管网网站提供的以上信息仅供参考,如有异议,请以权威部门公布的内容为准!
信管网致力于为广大信管从业人员、爱好者、大学生提供专业、高质量的课程和服务,解决其考试证书、技能提升和就业的需求。
信管网软考课程由信管网依托10年专业软考教研倾力打造,教材和资料参编作者和资深讲师坐镇,通过深研历年考试出题规律与考试大纲,深挖核心知识与高频考点,为学员考试保驾护航。面授、直播&录播,多种班型灵活学习,满足不同学员考证需求,降低课程学习难度,使学习效果事半功倍。
发表评论 查看完整评论 | |