C# 关于类的事件和委托
事件是C#中的又一个重要概念,在发生其他类或对象需要关注的事情时,
本类或对象可以通过事件来通知它们。
发送事件的类称为事件的发送者,而接受事件的类称为事件的订阅户。
C# 委托
委托使用的关键字为delegate。
class Program { public delegate void Mypaly(string str); //声明委托 class Play { private string game; public Play(string s) { game = s; } public void PlayStart(string name) //委托调用的方法1 { Console.WriteLine("“{0}”开始玩《{1}》!", name, game); } public void PlayClose(string name) //委托调用的方法2 { Console.WriteLine("“{0}”已关掉《{1}》!", name, game); } public void callMypaly(Mypaly d, string s) //调用委托中的方法 { d(s); } } static void Main(string[] args) { Play paly = new Play("魔兽争霸"); Mypaly mypalyStart = new Mypaly(paly.PlayStart); Mypaly mypalyClose = new Mypaly(paly.PlayClose); Mypaly mypaly = mypalyStart + mypalyClose; mypaly("张三"); //paly.callMypaly(mypalyStart, "张三"); //paly.callMypaly(mypalyClose, "张三"); Console.ReadKey(); } }
打印结果:
“张三”开始玩《魔兽争霸》!
“张三”已关掉《魔兽争霸》!
C# 事件中的委托
声明事件的关键字为event。
class Program { class Eventclass //先定义一个类 { public delegate void CustomEventHandler(object sender, EventArgs e); //⒈定义一个委托类型的对象 //用delegate数据类型声明事件,要用event关键字 public event CustomEventHandler CustomEvent; public event CustomEventHandler Load; public void InCustomEvent() //引用事件方法 { CustomEvent(this, EventArgs.Empty); } public void onLoad() { temp1: Console.BackgroundColor = ConsoleColor.Red; Load(this, EventArgs.Empty); string s = Console.ReadLine(); if (s != "yuping") { Console.WriteLine("You must type 'yuping' for change it !"); goto temp1; } else { Console.BackgroundColor = System.ConsoleColor.Black; Console.Clear(); } } public void CustomEvent1(object sender, EventArgs e) { Console.WriteLine("Fire Event"); } public void Load1(object sender, EventArgs e) { Console.WriteLine("Current background color is {0}. Please input:", System.Console.BackgroundColor.ToString()); } } static void Main(string[] args) { Eventclass myEventclass = new Eventclass(); //实例Eventclass类 myEventclass.CustomEvent += new Eventclass.CustomEventHandler(myEventclass.CustomEvent1); //⒉实例关联事件 myEventclass.InCustomEvent(); //⒊调用 myEventclass.Load += new Eventclass.CustomEventHandler(myEventclass.Load1); myEventclass.onLoad(); Console.ReadKey(); } }
打印结果:
Fire Event
Current background color is Red. Please input:
abc
You must type 'yuping' for change it !
Current background color is Red. Please input:
//举例:通过在Class2中定义并触发事件,在Class1中调用事件处理程序。 class Program { public class MyEventArgs : EventArgs //继承EventArgs 包含事件数据的类的基类 { public string str; } public delegate void MyEventHandler1(object sender, MyEventArgs e); //声明委托对象 class Class2 { public event MyEventHandler1 Event1; //定义事件 public void mEvent1(MyEventArgs e) //触发事件 { if (Event1 != null) { Event1(this, e); } } } class Class1 { public Class1(Class2 class2) //创建委托对象并包含事件处理函数 { MyEventHandler1 mh1 = new MyEventHandler1(Method1); class2.Event1 += mh1; //订阅事件 这里只能+= 或-= } public void Method1(object sender, MyEventArgs e) { Console.WriteLine("事件处理结果:" + e.str); } } static void Main(string[] args) { Class2 class2 = new Class2(); Class1 class1 = new Class1(class2); MyEventArgs my1 = new MyEventArgs(); my1.str = "aaa"; class2.mEvent1(my1); Console.ReadKey(); } }