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


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

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

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

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

    QuestionArgs.cs

    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. }  
    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

    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>  
    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

    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. }  
    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

    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. }  
    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

    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. }  
    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

    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. }  
    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

    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. }  
    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. }  

    运行结果:

  • 相关阅读:
    你为什么老是找不到满意的工作? 多思考自己的原因 !
    微信企业号开发之-如何获取secret 序列号
    可以免费自学编程的12个网站
    软件职业:聊聊学习这件事!
    致行业初学者:关于某些培训机构,老徐聊几句,也许对你们有用
    查看centos版本号
    配置mysql远程访问权限,大家可能掉过的那些坑~
    测试必备技能系列6:软件安装部署是最基本的能力!
    一个测试老鸟对职业技术交流群的几点看法
    为什么你一直找不到工作:因为你什么都想学,什么都不会!
  • 原文地址:https://www.cnblogs.com/gc2013/p/3928651.html
Copyright © 2020-2023  润新知