·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 一个简单的小例子让你明白c#中的委托-终于懂了!

一个简单的小例子让你明白c#中的委托-终于懂了!

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

模拟主持人发布一个问题,由多个嘉宾来回答这个问题。

 

分析:从需求中抽出Host (主持人) 类和Guests (嘉宾) 类。

作为问题的发布者,Host不知道问题如何解答。因此它只能发布这个事件,将事件委托给多个嘉宾去处理。因此在Host 类定义事件,在Guests类中定义事件的响应方法。通过多番委托的"+="将响应方法添加到事件列表中,最终 Host 类将触发这个事件。实现过程如下:

 

代码其实很少下面贴出来所有代码:

 

QuestionArgs.cs

 

view plaincopy to clipboardPRint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     public class QuestionArgs:EventArgs  
  9.     {  
  10.         public string Message { get; set; }  
  11.     }  
  12. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     public class QuestionArgs:EventArgs  
  9.     {  
  10.         public string Message { get; set; }  
  11.     }  
  12. }  

 

 

 

 

 

Program.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Host host = new Host();  
  13.             host.Name = "主持人";  
  14.             host.args.Message = "C#的事件如何实现的?";  
  15.             Guests[] gArray = new Guests[3]  
  16.             {  
  17.                 new GuestA(){Name = "张小三"},  
  18.                 new GuestB(){Name = "李小四"},  
  19.                 new GuestC(){Name = "王老五"}  
  20.             };  
  21.             //用+=号,将嘉宾的答题方法加入到委托链  
  22.             host.QuestionEvent += new QuestionHandler(gArray[0].answer);  
  23.             host.QuestionEvent += new QuestionHandler(gArray[1].answer);  
  24.             host.QuestionEvent += new QuestionHandler(gArray[2].answer);  
  25.   
  26.             //触发事件   
  27.             host.StartAnswer();  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }<span style="color:#ff0000;">  
  32. </span>  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Host host = new Host();  
  13.             host.Name = "主持人";  
  14.             host.args.Message = "C#的事件如何实现的?";  
  15.             Guests[] gArray = new Guests[3]  
  16.             {  
  17.                 new GuestA(){Name = "张小三"},  
  18.                 new GuestB(){Name = "李小四"},  
  19.                 new GuestC(){Name = "王老五"}  
  20.             };  
  21.             //用+=号,将嘉宾的答题方法加入到委托链  
  22.             host.QuestionEvent += new QuestionHandler(gArray[0].answer);  
  23.             host.QuestionEvent += new QuestionHandler(gArray[1].answer);  
  24.             host.QuestionEvent += new QuestionHandler(gArray[2].answer);  
  25.   
  26.             //触发事件  
  27.             host.StartAnswer();  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }<span style="color:#ff0000;">  
  32. </span>  



 

 

 

 

Host.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     public delegate void QuestionHandler(object sender,QuestionArgs e);  
  9.     public class Host  
  10.     {  
  11.         //定义一个事件   
  12.         public event QuestionHandler QuestionEvent;  
  13.         public QuestionArgs args { set; get; }  
  14.         public Host()  
  15.         {  
  16.             //初始化事件参数   
  17.             args = new QuestionArgs();  
  18.         }  
  19.         public string Name { get; set; }  
  20.         public void StartAnswer()  
  21.         {  
  22.             Console.WriteLine("开始答题");  
  23.             QuestionEvent(this, args);  
  24.         }  
  25.     }  
  26. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     public delegate void QuestionHandler(object sender,QuestionArgs e);  
  9.     public class Host  
  10.     {  
  11.         //定义一个事件  
  12.         public event QuestionHandler QuestionEvent;  
  13.         public QuestionArgs args { set; get; }  
  14.         public Host()  
  15.         {  
  16.             //初始化事件参数  
  17.             args = new QuestionArgs();  
  18.         }  
  19.         public string Name { get; set; }  
  20.         public void StartAnswer()  
  21.         {  
  22.             Console.WriteLine("开始答题");  
  23.             QuestionEvent(this, args);  
  24.         }  
  25.     }  
  26. }  



 

 

 

 

Guests.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     /// <summary>   
  9.     /// 父类   
  10.     /// </summary>   
  11.     public class Guests  
  12.     {  
  13.         /// <summary>   
  14.         /// 嘉宾姓名   
  15.         /// </summary>   
  16.         public string Name { get; set; }  
  17.   
  18.         public virtual void answer(object sender, QuestionArgs e)  
  19.         {  
  20.             Console.Write("事件的发出者:" + (sender as Host).Name);  
  21.             Console.WriteLine("问题是:" + e.Message);  
  22.         }  
  23.     }  
  24. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     /// <summary>  
  9.     /// 父类  
  10.     /// </summary>  
  11.     public class Guests  
  12.     {  
  13.         /// <summary>  
  14.         /// 嘉宾姓名  
  15.         /// </summary>  
  16.         public string Name { get; set; }  
  17.   
  18.         public virtual void answer(object sender, QuestionArgs e)  
  19.         {  
  20.             Console.Write("事件的发出者:" + (sender as Host).Name);  
  21.             Console.WriteLine("问题是:" + e.Message);  
  22.         }  
  23.     }  
  24. }  



 

 

 

 

GuestC.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestC:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}开始答题:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestC:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}开始答题:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  



 

 

 

 

GuestB.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestB:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}开始答题:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestB:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}开始答题:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  



 

 

 

 

GuestA.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestA:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}开始答题:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestA:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}开始答题:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  

 

 

运行结果: