事件 和 委托
收集的代码 事件和委托
委托会在后面赋值的时候被覆盖,事件赋值一个执行一个,一下收集的测试代码
----------------系统委托EventHandler
----------------------------事件(封装)
public event SearchHandler search;
public event SearchHandler Search {
add {
search += value;
}
remove {
search -= value;
}
}
--------------------------------------------------------------------简单的委托例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace T2 { //委托必须是参数类型和返回类型相同,并且用deletegate修饰 class Computer { public int Add(int x, int z) { return x + z; } public int Sub(int x, int z) { return x - z; } //属性委托 public delegate int ComputerHander(int x, int z); public ComputerHander ch=null; } //类委托 public delegate int ComputerHander(int x, int z); } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace T2 { class Program { static void Main(string[] args) { //Computer的例子 Computer c = new Computer(); //调用类委托 ComputerHander hander = c.Add; Console.WriteLine(hander(1,2)); //调用属性委托 c.ch = c.Sub; Console.WriteLine(c.ch(1, 2)); } } }
--------------------------------------------------------------用方法做参数的委托
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace T2 { //定义一个业务逻辑类 class HelloManager { //把委托作为方法的参数 public void SayHello(string name, HelloHander hander) { hander(name); } } //定义中国人类 class Chinaes { public static void ChinesHello(string name) { Console.WriteLine("ChinesHello," + name); } } //定义英国人类 class English { public static void EnglishHello(string name) { Console.WriteLine("EnglishHello," + name); } } //定义委托 public delegate void HelloHander(string name); } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace T2 { class Program { static void Main(string[] args) { //调用HelloManager的例子 HelloManager h = new HelloManager(); h.SayHello("哈哈",English.EnglishHello); } } }
------------------------------------------------------------------自定义事件
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace T2 { //自定义注册事件 //猫是事件原,所以事件类型要在猫类里面定义 class Cat { //定义委托 public delegate void CryHander(); //定义事件,委托类型 public event CryHander CryEvent; public void Cry() { Console.WriteLine("猫大叫一声..."); if (CryEvent != null) { CryEvent();//执行事件 } } } //老鼠 class Mouse { public void Run() { Console.WriteLine("老鼠逃跑了...."); } } //主人 class Master { public void Weak() { Console.WriteLine("主人醒了...."); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace T2 { class Program { static void Main(string[] args) { //动态注册事件 Cat cat = new Cat(); Mouse mouse = new Mouse(); Master master = new Master(); //订阅事件,多播性 cat.CryEvent += new Cat.CryHander(mouse.Run); cat.CryEvent += new Cat.CryHander(master.Weak); //触发事件 cat.Cry(); } } }
--------------------------------------带参数的事件
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace T2 { //事件类型 public class QuestionEvenArgs : EventArgs { public string QuestionMes{get; set; } } //委托 public delegate void QuestionHander(object sendr,QuestionEvenArgs e); //主持人类 public class Host { public event QuestionHander AnswerQuestion; public string Name { get; set; } public string Question { get; set; }//问题 //触发事件方法 public void Start() { Console.WriteLine("大家好,我是:" + Name); Console.WriteLine("今天的问题是:" + Question); Console.WriteLine("请各位嘉宾开始回答"); //通知所有嘉宾回答问题 if (AnswerQuestion != null) { AnswerQuestion(this, new QuestionEvenArgs { QuestionMes = Question }); } } } //嘉宾抽象类--父类 public abstract class Guest { public string Name { get; set; } public abstract void Answer(object sendr, QuestionEvenArgs e); } public class GuestA : Guest { public override void Answer(object sendr, QuestionEvenArgs e) { Console.WriteLine(Name + ",这个问题是我考虑一下!"); } } public class GuestB : Guest { public override void Answer(object sendr, QuestionEvenArgs e) { Console.WriteLine(Name + ",这个问题我知道!"); } } public class GuestC : Guest { public override void Answer(object sendr, QuestionEvenArgs e) { Host h = sendr as Host; Console.WriteLine(h.Name+",这个问题是"+e.QuestionMes+",我知道答案,但是我不告诉你"); } } public class Test { public static void Main(string[] args) { Host h = new Host { Name = "李咏", Question = "伦敦奥运什么时候开始?" }; GuestA a = new GuestA { Name = "AAA" }; GuestB b = new GuestB { Name = "BBB" }; GuestC c = new GuestC { Name = "CCC" }; h.AnswerQuestion += new QuestionHander(a.Answer); h.AnswerQuestion += new QuestionHander(b.Answer); h.AnswerQuestion += new QuestionHander(c.Answer); h.Start(); } } }