一个简单的小例子让你明白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 ...
随机推荐
- 【转】让 cocos2d-x 的 CCHttpRequest 支持https
肖锐(Cooki)个人原创,欢迎转载,转载请注明地址,肖锐(Cooki)的技术博客 http://blog.csdn.net/xiao0026 由于游戏用到了网络头像, 今天发现换成facebook ...
- Lighting System Design
题意:从小到大给出额定功率,给出该功率费用,和灯泡的数量和单价,现在灯泡能在比他额定功率大的功率运行,求让所有灯泡正常工作的最小费用 分析: 问题转化为求用哪几个功率运行灯泡最小费用,dp[i]前i个 ...
- HDU 5778 abs (BestCoder Round #85 C)素数筛+暴力
分析:y是一个无平方因子数的平方,所以可以从sqrt(x)向上向下枚举找到第一个无平方因子比较大小 大家可能觉得这样找过去暴力,但实际上无平方因子的分布式非常密集的,相关题目,可以参考 CDOJ:无平 ...
- 读《HTML5与CSS3权威指南(上册)》笔记
第二章 1.内容类型:“text/html”.DOCTYPE声明:<!DOCTYPE html>.指定字符编码:<meta charset="utf-8"> ...
- JSON 省市数据包括港澳
data: [{ name: "北京", cities: ["西城", "东城", "崇文", "宣武&quo ...
- CSS 高级:尺寸、分类、伪类、伪元素
CSS 尺寸:允许你控制元素的高度和宽度.同样,还允许你增加行间距. CSS 分类:允许你控制如何显示元素,设置图像显示于另一元素中的何处,相对于其正常位置来定位元素,使用绝对值来定位元素,以及元素的 ...
- Xcode 6 越狱开发基础
最近接触到XCode越狱开发的问题,越狱开发首先iphone设备得越狱,然后安装Appsync,安装之后,安装ipa将不再验证程序签名的有效性,不签名的程序也可以直接在设备上运行,只需要保证IPA本身 ...
- 【转】Nginx系列(五)--nginx+tomcat实现负载均衡
原博文出于: http://blog.csdn.net/liutengteng130/article/details/47129909 感谢! Nginx占有内存少,并发能力强,事实上Nginx ...
- Laravel Configuration
Introduction All of the configuration files for the Laravel framework are stored in the app/config d ...
- jstat用法
jstat(JVM Statistics Monitoring Tool)是用于监视虚拟机各种运行状态信息的命令行工具.它可以显示本地或者远程虚拟机进程中的类装载.内存.垃圾收集.JIT编译等运行数据 ...