一个简单的小例子让你明白c#中的委托-终于懂了!
模拟主持人发布一个问题,由多个嘉宾来回答这个问题。
分析:从需求中抽出Host (主持人) 类和Guests (嘉宾) 类。
作为问题的发布者,Host不知道问题如何解答。因此它只能发布这个事件,将事件委托给多个嘉宾去处理。因此在Host 类定义事件,在Guests类中定义事件的响应方法。通过多番委托的"+="将响应方法添加到事件列表中,最终 Host 类将触发这个事件。实现过程如下:
代码其实很少下面贴出来所有代码:
QuestionArgs.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- public class QuestionArgs:EventArgs
- {
- public string Message { get; set; }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- public class QuestionArgs:EventArgs
- {
- public string Message { get; set; }
- }
- }
Program.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Host host = new Host();
- host.Name = "主持人";
- host.args.Message = "C#的事件如何实现的?";
- Guests[] gArray = new Guests[3]
- {
- new GuestA(){Name = "张小三"},
- new GuestB(){Name = "李小四"},
- new GuestC(){Name = "王老五"}
- };
- //用+=号,将嘉宾的答题方法加入到委托链
- host.QuestionEvent += new QuestionHandler(gArray[0].answer);
- host.QuestionEvent += new QuestionHandler(gArray[1].answer);
- host.QuestionEvent += new QuestionHandler(gArray[2].answer);
- //触发事件
- host.StartAnswer();
- Console.ReadLine();
- }
- }
- }<span style="color:#ff0000;">
- </span>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Host host = new Host();
- host.Name = "主持人";
- host.args.Message = "C#的事件如何实现的?";
- Guests[] gArray = new Guests[3]
- {
- new GuestA(){Name = "张小三"},
- new GuestB(){Name = "李小四"},
- new GuestC(){Name = "王老五"}
- };
- //用+=号,将嘉宾的答题方法加入到委托链
- host.QuestionEvent += new QuestionHandler(gArray[0].answer);
- host.QuestionEvent += new QuestionHandler(gArray[1].answer);
- host.QuestionEvent += new QuestionHandler(gArray[2].answer);
- //触发事件
- host.StartAnswer();
- Console.ReadLine();
- }
- }
- }<span style="color:#ff0000;">
- </span>
Host.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- public delegate void QuestionHandler(object sender,QuestionArgs e);
- public class Host
- {
- //定义一个事件
- public event QuestionHandler QuestionEvent;
- public QuestionArgs args { set; get; }
- public Host()
- {
- //初始化事件参数
- args = new QuestionArgs();
- }
- public string Name { get; set; }
- public void StartAnswer()
- {
- Console.WriteLine("开始答题");
- QuestionEvent(this, args);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- public delegate void QuestionHandler(object sender,QuestionArgs e);
- public class Host
- {
- //定义一个事件
- public event QuestionHandler QuestionEvent;
- public QuestionArgs args { set; get; }
- public Host()
- {
- //初始化事件参数
- args = new QuestionArgs();
- }
- public string Name { get; set; }
- public void StartAnswer()
- {
- Console.WriteLine("开始答题");
- QuestionEvent(this, args);
- }
- }
- }
Guests.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- /// <summary>
- /// 父类
- /// </summary>
- public class Guests
- {
- /// <summary>
- /// 嘉宾姓名
- /// </summary>
- public string Name { get; set; }
- public virtual void answer(object sender, QuestionArgs e)
- {
- Console.Write("事件的发出者:" + (sender as Host).Name);
- Console.WriteLine("问题是:" + e.Message);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- /// <summary>
- /// 父类
- /// </summary>
- public class Guests
- {
- /// <summary>
- /// 嘉宾姓名
- /// </summary>
- public string Name { get; set; }
- public virtual void answer(object sender, QuestionArgs e)
- {
- Console.Write("事件的发出者:" + (sender as Host).Name);
- Console.WriteLine("问题是:" + e.Message);
- }
- }
- }
GuestC.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- class GuestC:Guests
- {
- public override void answer(object sender, QuestionArgs e)
- {
- base.answer(sender, e);
- Console.WriteLine("{0}开始答题:我不知道", this.Name);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- class GuestC:Guests
- {
- public override void answer(object sender, QuestionArgs e)
- {
- base.answer(sender, e);
- Console.WriteLine("{0}开始答题:我不知道", this.Name);
- }
- }
- }
GuestB.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- class GuestB:Guests
- {
- public override void answer(object sender, QuestionArgs e)
- {
- base.answer(sender, e);
- Console.WriteLine("{0}开始答题:我不知道", this.Name);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- class GuestB:Guests
- {
- public override void answer(object sender, QuestionArgs e)
- {
- base.answer(sender, e);
- Console.WriteLine("{0}开始答题:我不知道", this.Name);
- }
- }
- }
GuestA.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- class GuestA:Guests
- {
- public override void answer(object sender, QuestionArgs e)
- {
- base.answer(sender, e);
- Console.WriteLine("{0}开始答题:我不知道", this.Name);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace EventDemo
- {
- class GuestA:Guests
- {
- public override void answer(object sender, QuestionArgs e)
- {
- base.answer(sender, e);
- Console.WriteLine("{0}开始答题:我不知道", this.Name);
- }
- }
- }
运行结果:
一个简单的小例子让你明白c#中的委托-终于懂了!的更多相关文章
- Asp.net MVC4之 一个简单的小例子
练习: 新建一个mvc项目 要求: 有3个视图 Login Index Details 目的:感受一下MVC与传统WebForm的差异性 WebForm的请求模型 MVC请求模型 传统WebForm ...
- 一个有趣的小例子,带你入门协程模块-asyncio
一个有趣的小例子,带你入门协程模块-asyncio 上篇文章写了关于yield from的用法,简单的了解异步模式,[https://www.cnblogs.com/c-x-a/p/10106031. ...
- 一个简单的CORBA例子
因为对CORBA分析的需要,这里写一个简单的CORBA例子.从JDK1.2开始,JDK中集成了ORB的实现,本例子使用了JDK1.7,对于JDK1.2+应该都没有问题.这个例子实现一个简单的加减乘除的 ...
- 输出多行字符的一个简单JAVA小程序
public class JAVA { public static void main(String[] args) { System.out.println("-------------- ...
- 轻松创建nodejs服务器(1):一个简单nodejs服务器例子
这篇文章主要介绍了一个简单nodejs服务器例子,本文实现了一个简单的hello world例子,并展示如何运行这个服务器,需要的朋友可以参考下 我们先来实现一个简单的例子,hello world ...
- 使用Multiplayer Networking做一个简单的多人游戏例子-3/3(Unity3D开发之二十七)
使用Multiplayer Networking做一个简单的多人游戏例子-1/3 使用Multiplayer Networking做一个简单的多人游戏例子-2/3 使用Multiplayer Netw ...
- 使用Multiplayer Networking做一个简单的多人游戏例子-2/3(Unity3D开发之二十六)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51007512 ...
- 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 ...
- 一个简单的cmake例子
一个简单的cmake例子CMakeLists.txt,生成动态库文件,可以指定发布目录. 尚不支持: 1.交叉编译环境配置 2.添加依赖库 #在当前目录新建一个build目录,然后cd build ...
随机推荐
- table中嵌套table,如何用jquery来控制奇偶行颜色
总是要趁着自己还有记忆的时候,把该记录下来的都记录下来,着实是不敢恭维自己的记性. 相信很多时候,我们前端人员,经常会用到table里面的某个td中还嵌套着table,而这个时候还总要去弄奇偶行的颜色 ...
- geusture for chrome cfg
{ "name": "Chrome Gestures", "version": "1.13.4", "norm ...
- 打印Ibatis最终的SQL语句
在项目开发时都大家都希望将SQL在后台打印出来,以帮助开发以及后续的bug修改.如果用JDBC那么可以方便的打印,可使用ibatis就不知道怎么办了,最近在网上找了一段log4j的配置可以很保姆的处理 ...
- codeforces 696A Lorenzo Von Matterhorn 水题
这题一眼看就是水题,map随便计 然后我之所以发这个题解,是因为我用了log2()这个函数判断在哪一层 我只能说我真是太傻逼了,这个函数以前听人说有精度问题,还慢,为了图快用的,没想到被坑惨了,以后尽 ...
- KVO KVC
@interface FoodData : NSObject { NSString * foodName; float foodPrice; } @end ////////////////////// ...
- VC++6.0 MFC播放视频
注:需要在windows xp下才可以使用Windows Media Player插件,在windows 7下面会找不到该插件. 1.Windows Media Player控件的主要方法: 1)Ge ...
- CSS 3的display:盒类型详解
在CSS中,使用display属性来定义盒的类型.总体来说,盒类型分为两类:inline和block.如div默认是block,span默认是Inline.可以通过display修改默认的表现方式. ...
- Windows 8.1及Windows8 JDK环境变量配置
一.首先安装JDK JDK:http://www.oracle.com/technetwork/cn/java/javase/downloads/index.html 根据操作系统选择相应的版本 二. ...
- Hadoop-安装过程-单虚拟机版(伪分布式)(Ubuntu13.04版本下安装)
由于新装的Ubutu默认情况下,系统只安装了SSH客户端,需要自行安装SSH服务端 如何确定是否安装了SSH服务端? 可以通过命令ssh localhost,结果如下,即未安装SSH服务端: 安装 ...
- JSF 2.0 hello world example
In this tutorial, we will show you how to develop a JavaServer Faces (JSF) 2.0 hello world example, ...