有感于c#事件烧开水的例子,自己修改一些,附上源码,可以直接运行

事件理解过程有些绕,但过来了,就调理清晰了。

一般是做成三个类,一个主程序类,一个事件类,一个方法类,主程序注册了事件类和方法类后,主程序去触发事件,然后方法类给予回应。当然也可以都放在主程序类里实现,效果杠杠的。

本范例用于初学者快速理解事件,老码农请绕道。

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

namespace WindowsFormsApp3
{

    public partial class Form1 : Form
    {
         
            public Form1()
        {
            InitializeComponent();
        }

      

        private void button2_Click(object sender, EventArgs e)
        {
            Heater heater = new Heater();
            Alarm alarm = new Alarm();
            heater.BoilEvent += alarm.MakeAlert;    //注册热水器的加热事件,使其不为null(事件与方法绑定)
            //heater.BoilEvent += (new Alarm()).MakeAlert;   //给匿名对象注册方法
            heater.BoilEvent += Display.ShowMsg;       //注册静态方法,用该事件再绑定一个方法
            heater.BoilWater();   //烧水方法,会自动调用注册过对象的方法
        }
    }


    // 热水器
    public class Heater
    {
        private int temperature;
        public delegate void BoilHandler(int param);   //声明委托
        public event BoilHandler BoilEvent;        //声明事件,与委托同名

        // 烧水
        public void BoilWater()
        {
            for (int i = 0; i <= 100; i++)
            {
                temperature = i;

                if (temperature > 95)
                {
                    if (BoilEvent != null)
                    { //如果有对象注册
                        BoilEvent(temperature);  //调用所有注册对象的方法
                    }
                }
            }
        }
    }

    // 警报器
    public class Alarm
    {
        public void MakeAlert(int param)
        {
            string contern = "Alarm:嘀嘀嘀,水已经 " + param + "度了";
            //  Console.WriteLine("Alarm:嘀嘀嘀,水已经 {0} 度了:", param);
            MessageBox.Show(contern);
        }
    }

    // 显示器
    public class Display
    {
        public static void ShowMsg(int param)
        { //静态方法
          // Console.WriteLine("Display:水快烧开了,当前温度:{0}度。", param);
            MessageBox.Show("我是本事件绑定的第二个方法");
        }
    }

}
 

接下来有个更简单的例子,button就像加热的过程,注册了温度大于10的事件,超过就显示label内容

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

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        private int kk=0;
        private int temperature;

        public Form1()
        {
            InitializeComponent();
        }
        public delegate void BoilHandler(int param);   //声明委托
        public event BoilHandler BoilEvent;        //声明事件,与委托同名
        

        private void Form1_Load(object sender, EventArgs e)
        {
            BoilEvent += MakeAlert;    //注册热水器的加热事件,使其不为null(事件与方法绑定)

        }

        private void MakeAlert(int param)
        {
            if (kk >10 )
            {
                label3.Text = "大于10 了,第" + kk + "次";
            }
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            kk++;
            if (BoilEvent != null)//如果有对象注册
            { 
                BoilEvent(temperature);  //调用所有注册对象的方法
            }
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值