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

分析:从需求中抽出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. namespace EventDemo
  6. {
  7. public class QuestionArgs:EventArgs
  8. {
  9. public string Message { get; set; }
  10. }
  11. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. public class QuestionArgs:EventArgs
  8. {
  9. public string Message { get; set; }
  10. }
  11. }

Program.cs

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

Host.cs

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

Guests.cs

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

GuestC.cs

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

GuestB.cs

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

GuestA.cs

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

运行结果:

一个简单的小例子让你明白c#中的委托-终于懂了!的更多相关文章

  1. Asp.net MVC4之 一个简单的小例子

    练习: 新建一个mvc项目 要求: 有3个视图  Login Index Details 目的:感受一下MVC与传统WebForm的差异性 WebForm的请求模型 MVC请求模型 传统WebForm ...

  2. 一个有趣的小例子,带你入门协程模块-asyncio

    一个有趣的小例子,带你入门协程模块-asyncio 上篇文章写了关于yield from的用法,简单的了解异步模式,[https://www.cnblogs.com/c-x-a/p/10106031. ...

  3. 一个简单的CORBA例子

    因为对CORBA分析的需要,这里写一个简单的CORBA例子.从JDK1.2开始,JDK中集成了ORB的实现,本例子使用了JDK1.7,对于JDK1.2+应该都没有问题.这个例子实现一个简单的加减乘除的 ...

  4. 输出多行字符的一个简单JAVA小程序

    public class JAVA { public static void main(String[] args) { System.out.println("-------------- ...

  5. 轻松创建nodejs服务器(1):一个简单nodejs服务器例子

    这篇文章主要介绍了一个简单nodejs服务器例子,本文实现了一个简单的hello world例子,并展示如何运行这个服务器,需要的朋友可以参考下   我们先来实现一个简单的例子,hello world ...

  6. 使用Multiplayer Networking做一个简单的多人游戏例子-3/3(Unity3D开发之二十七)

    使用Multiplayer Networking做一个简单的多人游戏例子-1/3 使用Multiplayer Networking做一个简单的多人游戏例子-2/3 使用Multiplayer Netw ...

  7. 使用Multiplayer Networking做一个简单的多人游戏例子-2/3(Unity3D开发之二十六)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51007512 ...

  8. 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 ...

  9. 一个简单的cmake例子

    一个简单的cmake例子CMakeLists.txt,生成动态库文件,可以指定发布目录. 尚不支持: 1.交叉编译环境配置 2.添加依赖库   #在当前目录新建一个build目录,然后cd build ...

随机推荐

  1. 在android中进行视频的分割

    最近项目有个需求要对录制的视频进行分割,查了很多资料,看到ffmpeg可以对视频进行分割.上网找到别人基于android的开源ffmpeg,终于编译成功ffmpeg.so.但是要使用的话还要查ffmp ...

  2. HDU 5489 Removed Interval

    题意:求一段序列中删掉L个连续元素后的LIS. 解法:我的想法很复杂= =怎么说呢……首先用nlogn的方法求LIS得到的序列dp的第i项的意义为上升子序列所有长度为i的序列结尾元素的最小值,那么先倒 ...

  3. Redis Sentinel机制与用法

    概述 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端)都 ...

  4. JAX-RPC

    JAX-RPC(基于可扩展标记语言XML的远程过程调用的Java应用程序接口)是Java Web服务开发包(WSDP)的应用程序接口(API),WSDP能使Java开发者在Web服务或其他的Web应用 ...

  5. codeforces 678E Another Sith Tournament 概率dp

    奉上官方题解 然后直接写的记忆化搜索 #include <cstdio> #include <iostream> #include <ctime> #include ...

  6. 机器学习中的数学(3)-模型组合(Model Combining)之Boosting与Gradient Boosting

    版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...

  7. Google Appengine参考路径

    1.Hello, World! in 5 minutes 2.Creating a Guestbook -Introduction 3.Sample Applications 1.Programmin ...

  8. markdown常用html标签

    换行 测试<br />一下 效果:测试一下 标记 <mark>测试一下</mark> 效果:测试一下 空格         测试一下 效果:        测试一下 ...

  9. Signs of a poorly written jQuery plugin 翻译 (Jquery插件开发注意事项,Jquey官方推荐)

    原文链接:http://remysharp.com/2010/06/03/signs-of-a-poorly-written-jquery-plugin/ 原文作者:remy sharp So far ...

  10. wine的中文字体显示

    从1.1.4开始wine的界面就已经支持中文了,但是对于软件中的中文支持并不太好,主要原因.还是字体...Let's go 首先,copy一下字体:把simsun.ttc (即宋体)复制到 ~/.wi ...