委托事件你会用了吗?

来源:互联网 发布:马士兵 java web 编辑:程序博客网 时间:2024/04/28 20:29

在编程过程中如果两个事件的发生是一种紧密相连的关系,也就是说事件A的发生导致事件B的发生,那么我们就自然而然的想到将这两个事件用委托关联起来,类似于触发器。下面通过一个小例子简单了解一下委托的使用。假如我们想要做一个天气情况展示器,当天气情况改变的时候该展示器应该自动更新并将最新的天气情况展示出来。下面我们通过代码具体讲解一下委托是怎样使用的:

public class Weather    {        /// <summary>        /// Weather类的私有变量,wind代表风,rain代表雨,sun代表阳光        /// </summary>        private string wind;        private string rain;        private string sun;        public string Wind        {            get { return wind ;}            set { wind = value; }        }        public string Rain        {            get { return rain; }            set { rain  = value;}                    }        public string Sun        {            get { return sun; }            set { sun = value; }        }        /// <summary>        /// 声明一个委托        /// </summary>        public delegate void WeatherChanged();        /// <summary>        /// 声明一个委托类型的事件        /// </summary>        public event WeatherChanged WeatherChange;        //Weather的构造函数        public Weather ()                                                                                                                                                                                                                                                                                                                                                                     {                    }        //构造函数重载        public Weather(string wind, string rain, string sun)        {            this.Wind  = wind;            this.Rain  = Rain;            this.Sun = sun;        }        /// <summary>        /// 当调用Notify的时候将触发委托类型的事件        /// </summary>        public void Notify()        {            WeatherChange ( );        }    }


上面的代码中我们首先声明了一个委托public delegate void WeatherChanged()。我们可以将委托视为一个函数类,public event WeatherChanged WeatherChange表明我们声明了一个该委托类型的一个函数对象WeatherChange。Weather类有一个函数Notify(),该方法的作用是当天气情况发生改变的时候将给出天气变化通知,在方法体中我们可以看到WeatherChange函数对象,也就是说当调用Weather类对象的Notify()方法的时候将触发该委托类函数。下面我们再从客户端代码中讲解委托类函数是怎样使用的:

namespace WebTest{    public partial class _Default : System.Web.UI.Page    {        /// <summary>        /// 实例化一个Weather类对象        /// </summary>        Weather weather = new Weather("大雨","没太阳", "大风");        protected void Page_Load(object sender, EventArgs e)        {            //打印出当前天气状况            Response.Write(weather.Rain + "<br>");            Response.Write(weather.Sun + "<br>");            Response.Write(weather.Wind + "<br>");            //把weather的WeatherChange方法委托给WindChange方法            weather.WeatherChange += new Weather.WeatherChanged(WindChange);            //把weather的WeatherChange方法委托给RainChange方法            weather.WeatherChange += new Weather.WeatherChanged(RainChange);            //把weather的WeatherChange方法委托给SunChange方法            weather.WeatherChange += new Weather.WeatherChanged(SunChange);        }        public void  WindChange()        {            //设置当前风速情况            weather.Wind  = TextBox1 .Text ;            //打印出风情况发生改变            Response.Write("<br>风发生了改变");        }        public void   RainChange()        {            //设置当前的雨的情况            weather .Rain  = TextBox2 .Text ;            //打印出雨情况发生改变            Response.Write("<br>雨发生了改变");        }        public void   SunChange()        {            //设置当期阳光情况            weather.Sun  = TextBox3 .Text ;            //打印出阳光情况发生改变            Response.Write("<br>太阳发生了改变");        }        protected void Button1_Click(object sender, EventArgs e)        {            //点击按钮将显示出最新天气情况            weather.Notify();        }    }}

在客户端代码中我们可以看到 weather.WeatherChange += new Weather.WeatherChanged(WindChange)这样一条语句,该语句表名将委托类对象函数(这里的对象实际上指的是一个函数,因为委托就是一个函数类,所以该类实例化出的对象也就是一个方法了)委托给了WindChange函数。当我们点击Button1的时候将调用weather对象的Notify()函数,调用Notify()函数的时候将触发WeahterChange函数,由于在上面代码中我们将该函数委托给了WindChange函数,RainChange函数和SunChange函数,所以当调用Notify函数的时候将依次所有被委托的函数。但是在使用委托的时候我们一定要注意一定要先实例化委托函数再进行函数调用,否则便会出现“未将引用设置到实例”的错误。

原创粉丝点击