摘要:【Java代碼】classLight{//電燈類publicvoidturnLight(intdegree){//調(diào)整燈光亮度,0表示關(guān)燈,100表示亮度最大}};classTV{//電視機(jī)類publicvoidsetChannel(intchannel){//0表示關(guān)機(jī),1表示開(kāi)機(jī)并切換到1頻道}};interfaceCommand{//抽象命令類voidon();voidoff();};cl
【Java 代碼】
class Light{ //電燈類
public void turnLight(int degree){ //調(diào)整燈光亮度,0表示關(guān)燈,100表示亮度最大}
};
class TV{ //電視機(jī)類
public void setChannel(int channel){// 0表示關(guān)機(jī),1表示開(kāi)機(jī)并切換到1頻道 }
};
interface Command{ //抽象命令類
void on();
void off();
};
class RemoteController{ //遙控器類
protected Command []commands = new Command[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();
}
public void setCommand(int button, Command command){
(1) = command; //設(shè)置每個(gè)按鈕對(duì)應(yīng)的命令對(duì)象
}
};
class LightCommand implements Command{ //電燈命令類
protected Light light; //指向要控制的電燈對(duì)象
public void on(){light.turnLight(100);};
public void off(){light. (2);};
public LightCommand(Light light){this.light = light;};
};
class TVCommand implements Command{ //電視機(jī)命令類
protected TV tv; //指向要控制的電視機(jī)對(duì)象
public void on(){tv. (3);};
public void off(){tv.setChannel(0);};
public TVCommand(TV tv){this.tv = tv;};
};
軟考備考資料免費(fèi)領(lǐng)取
去領(lǐng)取
共收錄117.93萬(wàn)道題
已有25.02萬(wàn)小伙伴參與做題