具体定义就不说了(我自己也搞不懂) 面试时经常会考一个问题 这里直接贴出答案:事件是委托的 一种特殊形式。OK,先写下需求(摘自大话设计模式_推荐新手阅读):
需求: 有一只猫叫Tom 有两只老鼠 jack loose ,Tom只要叫一声 “喵,我来了” 两只老鼠就说“老猫来了,快跑”
分析: 很显然应该有 Cat和Mouse两个类, 当Cat的Shout出发时,Mouse就执行Run函数,不过这里如何让Shout执行时通知两只老鼠呢? 显然老猫不认识老鼠,也不会主动通知老鼠它们“我来了,你们快跑”,所以Cat类中是不应该关联Mouse类的。此时用委托最好的处理方法了。 代码如下:
在Cat类中:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; namespace Delegate_Event_Demo { public class Cat { private string m_name = ""; public Cat(string pName) { this.m_name = pName; } /// <summary> /// 声明委托 /// </summary> public delegate void CatShoutEventHandler(object sender,EventArgs args); /// <summary> /// 声明CatShoutEventHandler类型事件 /// </summary> // public event EventHandler<ObjectEventArgs<string>> OnCatShoutEvent; public event EventHandler OnCatShoutEvent; public void Shout() { MessageBox.Show("喵,我来了"); if (OnCatShoutEvent != null) { //为什么OnCatShoutEvent是无参数无返回值的呢? //因为事件OnCatShoutEvent的类型是委托CatShoutEventHandler,而CatShoutEventHandler就是无参数无返回值的 OnCatShoutEvent(this,new EventArgs()); } } } }
定义委托用关键字delegate 声明事件用关键字event 事件的名称一般用On开头Event结尾(微软习惯命名方式)
Mouse类 :
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; namespace Delegate_Event_Demo { public class Mouse { private string m_name = ""; public Mouse(string pName) { this.m_name = pName; } //public void Run(object sender, ObjectEventArgs<string> e) //{ // MessageBox.Show("老猫"+e.Value+"来了,"+this.m_name+"快跑"); //} public void Run() { MessageBox.Show("老猫来了," + this.m_name + "快跑"); } } }
MainWindow 中:
using System.Windows; namespace Delegate_Event_Demo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Cat cat = new Cat("Tom"); // cat.OnCatShoutEvent += new System.EventHandler<ObjectEventArgs<string>>(cat_OnCatShoutEvent); cat.OnCatShoutEvent += new System.EventHandler(cat_OnCatShoutEvent); cat.Shout(); } void cat_OnCatShoutEvent(object sender, System.EventArgs e) { Mouse mouse2 = new Mouse("loose"); mouse2.Run(); } //void cat_OnCatShoutEvent(object sender, ObjectEventArgs<string> e) //{ // Mouse mouse2 = new Mouse("loose"); // mouse2.Run(sender, e); //} } }
OK 到这里编码就结束了 运行程序即可。
当然如果想在事件中传递参数怎么办 那只需在增加一个类
CatShoutEventArgs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Delegate_Event_Demo { public class CatShoutEventArgs:EventArgs { //private string m_name = ""; //public string Name //{ // get { return m_name; } // set { m_name = value; } //} //这是一种简写与上面代码效果一样(若不需要默认值即可这样写) public string Name { get; set; } } }
上面的三个类都要做出相应修改:
Cat类:
/// <summary> /// 声明委托 /// </summary> public delegate void CatShoutEventHandler(object sender,CatShoutEventArgs args);
public void Shout() { MessageBox.Show("喵,我来了"); if (OnCatShoutEvent != null) { //为什么OnCatShoutEvent是无参数无返回值的呢? //因为事件OnCatShoutEvent的类型是委托CatShoutEventHandler,而CatShoutEventHandler就是无参数无返回值的 CatShoutEventArgs e = new CatShoutEventArgs(); e.Name = this.m_name; OnCatShoutEvent(this, e); } }
Mouse类:
public void Run(CatShoutEventArgs args) { MessageBox.Show(args.Name+ "老猫来了," + this.m_name + "快跑"); }
MainWindow:
public MainWindow() { InitializeComponent(); Cat cat = new Cat("Tom"); // cat.OnCatShoutEvent += new System.EventHandler<ObjectEventArgs<string>>(cat_OnCatShoutEvent); cat.OnCatShoutEvent += new Cat.CatShoutEventHandler(cat_OnCatShoutEvent); cat.Shout(); } void cat_OnCatShoutEvent(object sender, CatShoutEventArgs args) { Mouse mouse2 = new Mouse("loose"); mouse2.Run(args); }