摘要:【Java代码】classLight{//电灯类publicvoidturnLight(intdegree){//调整灯光亮度,0表示关灯,100表示亮度最大}};classTV{//电视机类publicvoidsetChannel(intchannel){//0表示关机,1表示开机并切换到1频道}};interfaceCommand{//抽象命令类voidon();voidoff();};cl
【Java 代码】
class Light{ //电灯类
public void turnLight(int degree){ //调整灯光亮度,0表示关灯,100表示亮度最大}
};
class TV{ //电视机类
public void setChannel(int channel){// 0表示关机,1表示开机并切换到1频道 }
};
interface Command{ //抽象命令类
void on();
void off();
};
class RemoteController{ //遥控器类
protected Command []commands = new Command[4];
//遥控器有4个按钮,按照编号分别对应4个Command对象
public void onPressButton(int button){
//按钮被按下时执行命令对象中的命令
if(button % 2 == 0)commands[button].on();
else commands[button].off();
}
public void setCommand(int button, Command command){
(1) = command; //设置每个按钮对应的命令对象
}
};
class LightCommand implements Command{ //电灯命令类
protected Light light; //指向要控制的电灯对象
public void on(){light.turnLight(100);};
public void off(){light. (2);};
public LightCommand(Light light){this.light = light;};
};
class TVCommand implements Command{ //电视机命令类
protected TV tv; //指向要控制的电视机对象
public void on(){tv. (3);};
public void off(){tv.setChannel(0);};
public TVCommand(TV tv){this.tv = tv;};
};
软考备考资料免费领取
去领取