摘要:【C++代碼】classLight{//電燈類public:voidturnLight(intdegree){//調(diào)整燈光亮度,0表示關(guān)燈,100表示亮度最大};};classTV{//電視機(jī)類public:voidsetChannel(intchannel){//調(diào)整頻道,0表示關(guān)機(jī),1表示開(kāi)機(jī)并切換到1頻道};};classCommand{//抽象命令類public:virtualvoidon()=
【C++代碼】
class Light{ //電燈類
public:
void turnLight(int degree){ //調(diào)整燈光亮度,0表示關(guān)燈,100表示亮度最大};
};
class TV{ //電視機(jī)類
public:
void setChannel(int channel){//調(diào)整頻道,0表示關(guān)機(jī),1表示開(kāi)機(jī)并切換到1頻道};
};
class Command{ //抽象命令類
public:
virtual void on()=0;
virtual void off()=0;
};
class RemoteController{ //遙控器類
protected:
Command *commands[4]; //遙控器有4個(gè)按鈕,按照編號(hào)分別對(duì)應(yīng)4個(gè)Command對(duì)象
public:
void onPressButton(int button){ //按鈕被按下時(shí)執(zhí)行命令對(duì)象中的命令
if(button % 2 == 0)commands[button]->on();
else commands[button]->off();
}
void setCommand(int button,Command * command){
(1) = command; //設(shè)置每個(gè)按鈕對(duì)應(yīng)的命令對(duì)象
}
};
class LightCommand : public Command{ //電燈命令類
protected: Light *light; //指向要控制的電燈對(duì)象
public:
void on(){light->turnLight(100);};
void off(){light->(2);};
LightCommand(Light * light){this->light = light;};
};
class TVCommand : public Command{ //電視機(jī)命令類
protected: TV * tv; //指向要控制的電視機(jī)對(duì)象
public:
void on(){tv->(3);};
軟考備考資料免費(fèi)領(lǐng)取
去領(lǐng)取
共收錄117.93萬(wàn)道題
已有25.02萬(wàn)小伙伴參與做題