电器遥控器

 Main:

          ; 创建遥控器对象
           panel:= new ControlPanel()
           airCondition:= new AirCondition()      ;创建空调对象

          ; 创建Command对象,传递空调对象
           onCommand:= new AirOnCommand(airCondition)
           offCommand:= new AirOffCommand(airCondition)

          ; 设置遥控器的Command
           panel.SetCommand(onCommand, offCommand)

           panel.PressOn()     ;按下On按钮,开空调,温度调到16度
           panel.PressOff()    ;按下Off按钮,关空调

return

class Light{
       TurnOn(){
           msgbox "The light is turned on."
       }
       TurnOff() {
           msgbox "The light is turned off."
       }
    }

   ;定义风扇
    class Fan {
       Start() {
           msgbox "The fan is starting."
       }
       Stop() {
           msgbox "The fan is stopping."
       }
    }

    ;定义门
    class Door {
       Open() {
           msgbox "The door is open for you."
       }
       Shut() {
           msgbox "The door is closed for safety"
       }
    }
;定义空调,用于测试给遥控器添新控制类型
class AirCondition
{
       Start()
	   {
           msgbox  "The AirCondition is turned on." 
       }
       SetTemperature(i)
	   {
           msgbox  % "The temperature is set to " .  i 
       }
       Stop()
	   {
           msgbox "The AirCondition is turned off."
       }
    }
    


   ; 定义开空调命令
    class AirOnCommand
	{
       __New(airCondition)  ;构造函数必须用__new
	   {
           this.airCondition:=airCondition
       }
       Execute()
	   {
	   ;注意,你可以在Execute()中添加多个方法
           this.airCondition.Start()
           this.airCondition.SetTemperature(16)
       }
    }

   ; 定义关空调命令
    class AirOffCommand
	{
       __New(airCondition) {
           this.airCondition := airCondition
       }
       Execute() {
           this.airCondition.Stop()
       }
    }

    class DeviceCommand
	{
		
	}


   ; 定义遥控器
    class ControlPanel
	{
		 ;设置多功能遥控器控制哪个电器
       SetDevice(device) {
           this.device:= device
       }
       PressOn()
	   {
           this.onCommand.Execute()
       }

       PressOff()
	   {
           this.offCommand.Execute()
       }

       SetCommand(onCommand,offCommand)
	   {
           this.onCommand := onCommand
           this.offCommand := offCommand
       }
    }


给TA捐赠
共{{data.count}}人
人已捐赠
其他

用图片做光标(很好玩的,试试吧!)

2020-3-9 4:20:44

其他案例

电影天堂

2020-3-9 4:22:44

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索