zoukankan      html  css  js  c++  java
  • c++编程题之空调遥控器

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    class Light //电灯类
    {
    public:
        void turnLight(int degree){
            //调整灯光亮度,0表示关灯,100表示亮度最大
        }
    };
    
    class TV //电视机类
    {
    public:
        void setChannel(int channel){ //调整电视频道
            //0表示关机
            //1表示开机并切换到1频道
        }
    };
    
    class Command //抽象命令类
    {
    public:
        virtual void on()=0;
        virtual void off()=0;
    };
    
    class RemoteController
    {
    protected:
        Command *commands[4];//四个遥控器按钮
    public:
        void onPressButton(int button)
        {
            if (button%2 == 0)
            {
                commands[button]->on();
            }
            else
            {
                commands[button]->off();
            }
        }
        void setCommand(int button, Command *command)
        {
            commands[button] = command;
        }
    };
    
    class LightCommand : public Command
    {
    protected:
        Light *light;
    public:
        LightCommand(Light *light){this->light = light;}
        void on(){light->turnLight(100);}
        void off(){light->turnLight(0);}
    };
    
    class TVCommand : public Command
    {
    protected:
        TV *tv;
    public:
        TVCommand(TV *tv){this->tv = tv;}
        void on(){tv->setChannel(1);}
        void off(){tv->setChannel(0);}
    };
    
    int main(int argc, char const *argv[])
    {
        Light light;
        TV tv;
        LightCommand lightCommand(&light);
        TVCommand tvCommand(&tv);
        RemoteController remoteController;
        remoteController.setCommand(0, &lightCommand);
        remoteController.setCommand(1, &lightCommand);
        remoteController.setCommand(2, &tvCommand);
        remoteController.setCommand(3, &tvCommand);
    
        return 0;
    }
    
    
  • 相关阅读:
    es5核心技术
    es6 迭代器 和 生成器 学习笔记
    nodejs 基础学习笔记
    node 基本原理
    mac php7 连接数据库遇到的问题
    express ,koa1, koa2学习笔记
    mac mysql的安装
    webpack 给css添加前缀
    利用git将本地的代码同步到github上
    vuex 学习总结及demo
  • 原文地址:https://www.cnblogs.com/Chlik/p/13541297.html
Copyright © 2011-2022 走看看