首 页 行业热点 新车 试驾评测 养车用车 车型库

C# “空调-遥控器”仿真程序

发布网友

我来回答

3个回答

热心网友

写好了,两个类分别为:

 public enum Mode
    {
        Heating,
        Cooling
    }

    public enum UpDown
    {
        Up,
        Down
    }

    public class AirConditioner
    {
        private bool powerOn;

        private Mode currentMode;

        private int currentTemp;

        public AirConditioner()
        {
            this.powerOn = false;
            this.currentMode = Mode.Cooling;
            this.currentTemp = 16;
        }

        public void SwichPower()
        {
            this.powerOn = !powerOn;
        }

        internal void SetMode(Mode mode)
        {
            this.currentMode = mode;
        }

        internal void SetTemp(UpDown upDown)
        {
            switch (upDown)
            {
                case UpDown.Up:
                    if (this.currentTemp <= 30)
                    {
                        this.currentTemp++;
                    }
                    break;
                case UpDown.Down:
                    if (this.currentTemp >= 16)
                    {
                        this.currentTemp--;
                    }
                    break;
            }
        }

        public override string ToString()
        {
            return string.Format("Current Status:\r\nPower: {0}\r\nMode: {1}\r\nTemp: {2}", this.powerOn ? "On" : "Off", this.currentMode, this.currentTemp);
        }
    }

public class RemoteController
    {
        private AirConditioner conditioner;

        public RemoteController()
        {
            this.conditioner = new AirConditioner();
        }

        public void SwitchPower()
        {
            this.conditioner.SwichPower();
            Console.WriteLine(this.conditioner);
        }

        public void SetMode(Mode mode)
        {
            this.conditioner.SetMode(mode);
            Console.WriteLine(this.conditioner);

        }

        public void SetTemp(UpDown upDown)
        {
            this.conditioner.SetTemp(upDown);
            Console.WriteLine(this.conditioner);
        }
    }

值得注意的是,“通过直接操作空调不能进行调节温度、改变模式(制热、制冷)。”这一条说明这些方法应该不允许被声明为public,但是又要能够被遥控器访问,所以应该声明为internal


测试代码:

 static void Main(string[] args)
        {
            var controller = new RemoteController();
            Console.WriteLine("Turn on the conditioner...\r\n");
            controller.SwitchPower();
            Console.WriteLine("================================================================");
            Console.WriteLine("Turn off the conditioner...\r\n");
            controller.SwitchPower();
            Console.WriteLine("================================================================");
            Console.WriteLine("Set the mode as \"cooling\"...\r\n");
            controller.SetMode(Mode.Cooling);
            Console.WriteLine("================================================================");
            Console.WriteLine("Set the mode as \"Heating\"...\r\n");
            controller.SetMode(Mode.Heating);
            Console.WriteLine("================================================================");
            Console.WriteLine("Turn up temp...\r\n");
            controller.SetTemp(UpDown.Up);
            Console.WriteLine("================================================================");
            Console.WriteLine("Turn down temp...\r\n");
            controller.SetTemp(UpDown.Down);
        }

运行结果:



源码在附件中,如有疑问,欢迎追问。

热心网友

你好!!
不明白这一条?? 6、 不允许写键盘输入的代码!不允许设计图形界面!

热心网友

需求分析:
前台界面,分为屏显区域和遥控按键区域。通过屏显显示现在空调的状态,通过按键改变状态。
代码设计,空调类,字段包括开关状态、温度、模式、属性和字段相对应,方法包括开机、关机、、制冷模式、制热模式、屏幕显示。
遥控器类继承空调类,方法包括重写开机、关机、制冷模式、制热模式、屏幕显示,增加温度、降低温度。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com