ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

20210430 C#课堂练习代码 热水器事件

2021-07-06 01:33:52  阅读:283  来源: 互联网

标签:课堂练习 C# void System 热水器 20210430 public 水开 温度


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        热水器 我的热水器 = new 热水器();
        警报器 我的警报器 = new 警报器();
        显示器 我的显示器 = new 显示器();
        public Form1()
        {
            InitializeComponent();

            我的热水器.水开了事件 += 我的警报器.报警;

            我的热水器.水开了事件 += 我的显示器.显示水温;

            我的热水器.温度上升事件 += 我的显示器.显示水温;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "加热")
            {
                button1.Text = "暂停";
                timer1.Enabled = true;
            }
            else
            {
                button1.Text = "加热";
                timer1.Enabled = false;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            我的热水器.烧水();
            textBox1.Text = 我的警报器.警报信息;
            textBox1.Text += 我的显示器.字符串;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            我的热水器.变冷();
            textBox1.Text = 我的警报器.警报信息;
            textBox1.Text += 我的显示器.字符串;
        }
    }
    class 热水器
    {
        
        int 温度=90;
        public delegate void 水开了委托类型(int 温度);
        public event 水开了委托类型 水开了事件;

        public delegate void 温度上升委托类型(int 温度);
        public event 温度上升委托类型 温度上升事件;

        public void 烧水()
        {


                温度++;
                if (温度上升事件 != null)
                    温度上升事件(温度);

                if (温度 > 96)
                {
                    //发布事件:水开了
                    if (水开了事件 != null)
                        水开了事件(温度);
                    System.Media.SystemSounds.Beep.Play(); 
                }

        }
        public void 变冷()
        {
            温度--;
            if (温度上升事件 != null)
                温度上升事件(温度);
        }
    }
    class 警报器
    {
        public string 警报信息;
        public void 报警(int 温度)
        {
            警报信息= "水开了,温度=" + 温度.ToString();
        }
    }
    class 显示器
    {
        public string 字符串;
        public void 显示水温(int 温度)
        {
            字符串 ="温度=" + 温度.ToString();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            热水器 我的热水器 = new 热水器();
            警报器 我的警报器 = new 警报器();
            我的热水器.水开了事件 += 我的警报器.报警;

            我的热水器.水开了事件 += 显示器.显示水温;

            我的热水器.温度上升事件 += 显示器.显示水温;
            我的热水器.烧水();
            Console.ReadLine();
        }
    }
    
    class 热水器
    {
        int 温度;
        public delegate void 水开了委托类型(int 温度);
        public event 水开了委托类型 水开了事件;

        public delegate void 温度上升委托类型(int 温度);
        public event 温度上升委托类型 温度上升事件;

        public void 烧水(){

            for (int k = 0; k < 100; k++)
            {
                for (int i = 0; i < 100000; i++) ;
                温度++;
                if (温度上升事件 != null)
                    温度上升事件(温度);

                if(温度>96){
                    //发布事件:水开了
                    if(水开了事件!=null)
                        水开了事件(温度);
                }
            }

        }
    }
    class 警报器
    {
        public void 报警(int 温度){
            Console.WriteLine("水开了,温度=" + 温度.ToString());
        }
    }
    class 显示器
    {
        public static void 显示水温(int 温度){
            Console.WriteLine("温度=" + 温度.ToString());
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 事件
{
    class Program
    {
        static void Main(string[] args)
        {
            热水器类 热水器 = new 热水器类();
            警报器类 警报器 = new 警报器类();
            热水器.水开了事件 += 警报器.发警告;
            热水器.水开了事件 += 显示类.显示信息静态方法;
            热水器.烧水();
            Console.ReadKey();
        }
    }

    public class 热水器类
    {
        private int 温度;
        public delegate void 开水委托(int param);
        public event 开水委托 水开了事件;
        public void 烧水()
        {
            for (int i = 0; i <= 96; i++)
            {
                温度 = i;
                if (温度 > 96)
                {
                    if (水开了事件 != null)
                    {
                        水开了事件(温度);
                    }

                }
            }
        }
    }
    public class 警报器类
    {
        public void 发警告(int 温度值)
        {
            Console.WriteLine("\a\a\a水要开了,温度{0}", 温度值);
            Console.ReadKey();
        }
    }

    public class 显示类
    {
        public static void 显示信息静态方法(int param)
        {
            Console.WriteLine("显示,水要开了,温度{0}", param);
        }
    }
}
`

标签:课堂练习,C#,void,System,热水器,20210430,public,水开,温度
来源: https://www.cnblogs.com/iamrich/p/14975077.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有