zoukankan      html  css  js  c++  java
  • 一个简单的小例子让你明白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. }  

    运行结果:

  • 相关阅读:
    gdb查看内存(转)
    c++ 前置++与后置++的区别
    stl第二级空间配置器详解(1)
    stl空间配置器简介
    套接字描述符就绪条件
    针对TCP连接异常断开的分析
    linux僵尸进程产生的原因以及如何避免产生僵尸进程
    k8s istio 配置域名转发到外部节点机器上
    tinymce增加mathjax 支持数学公式录入渲染
    vue 配置 TinyMCE
  • 原文地址:https://www.cnblogs.com/gc2013/p/3928651.html
Copyright © 2011-2022 走看看