一个简单的小例子让你明白c#中的委托-终于懂了!
2018-06-17 19:56:59来源:未知 阅读 ()
模拟主持人发布一个问题,由多个嘉宾来回答这个问题。
分析:从需求中抽出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);
- }
- }
- }
运行结果:
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- PHP简单实现单点登录功能示例 2019-10-09
- Linux简介及最常用命令(简单易学能解决95%以上的问题) 2019-10-08
- 面试官问我:一个 TCP 连接可以发多少个 HTTP 请求?我竟然 2019-09-30
- 提问频率较高的php面试题 2019-09-23
- PHP四大主流框架的优缺点,第一个最难 2019-09-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash