问题 填空题

阅读下列说明和C++代码。

[说明]

某大型商场内安装了多个简易的纸巾售卖机,自动出售2元钱一包的纸巾,且每次仅售出一包纸巾。纸巾售卖机的状态图如图5-1所示。

采用状态(State)模式来实现该纸巾售卖机,得到如图5-2所示的类图。其中类State为抽象类,定义了投币、退币、出纸巾等方法接口。类SoldState、SoldOutState、NoQuarterState和HasQuarterState分别对应图5-1中纸巾售卖机的4种状态:售出纸巾、纸巾售完、没有投币、有2元钱。

[C++代码]

#include <iostream>

using namespace std;

//以下为类的定义部分

class TissueMachine; //类的提前引用

class State

public:

virtual void insertQuarter()=0; //投币

virtual void ejectQuarter()=0; //退币

virtual void turnCrank()=0; //按下“出纸巾”按钮

virtual void dispense()=0; //出纸巾

;

/*类SoldOutState、NoQuarterState、HasQuarterState、SoldState的定义省略,

每个类中均定义了私有数据成员TissueMachine* tissueMachine;*/

class TissueMachine

private:

(1) *soldOutState, *noQuarterState, *hasQuarterState,*soldState,

*state;

int count; //纸巾数

public:

TissueMachine (int numbers);

void setState (State* state);

State* getHasQuarterState();

State* getNoQuarterState()j

State* getSoldState();

State* getSoldOutState();

int getCount()j

//其余代码省略

;

//以下为类的实现部分

void NoQuarterState ::insertQuarter()

tissueMachine->setState( (2) );

void HasQuarterState ::ejectQuarter()

tissueMachine->setState( (3) );

void SoldState ::dispense()

if (tissueMachine->getCount()>0)

tissueMachine->setState( (4) );

else

tissueMachine->setState( (5) );

//其余代码省略

(5)处应填()。

答案

参考答案:tissueMachine->getSoldoutState()

解析:

[分析]: 本题考查状态(State)模式的概念及应用。

状态模式是一种对象的行为型模式,允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类。状态模式的类图如下所示:

状态模式主要解决的是控制一个对象转换的条件表达式过于复杂的情况。把状态的判断逻辑转移到表示不同状态的一系列类当中,可以把复杂的判断逻辑简化。状态模式的好处是将与特定状态相关的行为局部化,并且将不同状态的行为分割开来。

题目利用状态模式来实现一个简易的纸巾售卖机。售卖机的状态转换图已经在题目中给出,类S01dState、SoldoutState、NoQuarterState和HasQuaerState分别用来表示售卖机的4种不同状态,对应于状态模式中的ConcreteState1,...ConcreteStateN。题目所设置的填空,主要集中在状态转换上。因此解答该题时,要求在理解状态模式内涵的基础上,依据纸巾售卖机的状态转换原则,给出正确的状态设置。

空(1)出现在类TissueMachine的数据成员定义部分。状态模式封装了状态的转换过程,但是它需要枚举可能的状态,因此需要确定状态种类。因此在类TissueMachine中需定义出所有可能的状态对象。根据所给出的对象名称及说明中的描述,可知(1)处应填入的类名为State。

空(2)~(5)都是与状态转换相关的,要求填写类TissueMachine中的方法setState在不同调用处的实际参数。根据方法的名称及调用方式,可以推断出这个方法的功能就是设置自动售卖机的当前状态。要填出这些空,只要对照图5.1的状态转换图,根据状态转换的条件确定出当前状态及下一状态即可。

空(2)出现在方法insertQuaner内,即给纸巾售卖机投入2元钱。根据状态图,“投入2元钱”之后,售卖机应转换到“有2元钱”的状态。“有2元钱”对应的状态的类为“HasQuanerState”,所以空(2)处应填类HasQuanerState的对象。由于hasQuanerState是类TissueMachine的私有数据成员,不能直接访问,所以只能通过调用相关的get方法来获取该对象。由此得出(2)应填tissueMachine->getHasQuarterState();

同理,空(3)表示的状态是从“有2元钱”状态,经历“退回2元钱”事件之后的状态,及“没有投币”状态。所以空(3)处应填tissueMachine->getNoQuanerState()。

空(4)和(5)处分别表示卖出一包纸巾之后,售卖机应该转换到的下一个状态。这个跟售卖机中的纸巾数有关,如果还有纸巾,则转换到“没有投币”状态,如果没有纸巾了,则转换到“纸巾售完”状态,因此,空(4)处应填tissueMachine->getNoQuarterState(),空(5)处应填tissueMachine->getSoldOutState()。

阅读理解

You may know the English letters A, B and C. But do you know there are people called ABC? You may like eating bananas. But did you know there is a “banana person”? How strange! Are these people from “another Earth”? No. They are just Chinese people like you and me.

ABC means American-Born Chinese. An ABC is a Chinese, but was born in the United States. Sometimes, people call an ABC a “banana person”. A banana is yellow outside and white inside. So, when a person is a banana, he or she is white inside—thinking like a Westerner and yellow outside—looking like a Chinese.

Do you know why? Usually, ABCs know little about China or the Chinese language. Some of them don’t speak Chinese.

But if ABCs cannot speak Chinese, can we still call them Chinese people? Yes, of course. They are Chinese. They are overseas (海外) Chinese. These people may be citizens(公民) of another country like the US, Canada or Singapore. But they have Chinese blood. Their parents, grandparents or even great-grandparents were from China. They all have black eyes and black hair.

But they are not Chinese citizens. They are not the people of the People’s Republic of China. For example, we all know the famous scientist  C.N. Yang(杨振宁). He got the Nobel Prize for Physics in 1957. The Chinese love him, but he is an American citizen.

小题1:What’s the author’s purpose in writing this passage?

A.He wants to tell us something about “ABC”.

B.He wants to show that Chinese are well respected in America.

C.He wants to tell us some knowledge about the English language.

D.He wants to introduce the American culture to us.小题2: Chinese in Western countries are called “banana persons” because ____.

A.their bodies are white inside but yellow outside

B.they think like Westerners but look like Chinese

C.they were born in China but go to study in America

D.they like to eat bananas小题3:C.N. Yang is mentioned here to show that ____.

A. American Chinese are great.        B. we love American Chinese

C. The Chinese can win Nobel Prizes   D. American Chinese are not Chinese citizens

多项选择题