C#_delegate - 用委托实现事件,Display和Log类都使用Clock对象
2013-09-24 23:57
716 查看
//public event SecondChangeHandler OnSecondChange; 若将委托加上event,则视作是事件,不是委托,外围就不能直接对OnSecondChange传值
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace EventClock { //一个存放事件信息的类 //存放这Clock类中的信息,和其他状态信息 public class TimeInfoEventArg : EventArgs { public TimeInfoEventArg(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } public readonly int hour; public readonly int minute; public readonly int second; } //中心类:其他类要订阅的类 //发布一个委托 //OnSecondChange public class Clock { private int hour; private int minute; private int second; //订阅这必须实现的委托 public delegate void SecondChangeHandler(object clcok, TimeInfoEventArg timeInformation); //委托实例 public SecondChangeHandler OnSecondChange; //设置始终运行,每一秒都触发一个事件 public void Run() { for (; ; ) { //休眠10秒 Thread.Sleep(10); //获取当前时间 System.DateTime dt = System.DateTime.Now; //如秒钟改变,通知订阅者 if (dt.Second != second) { //创建TimeInfoEventArg对象,传给订阅者 TimeInfoEventArg timeInformation = new TimeInfoEventArg(dt.Hour,dt.Minute,dt.Second); //如果有订阅者,通知他们 if (OnSecondChange != null) { OnSecondChange(this, timeInformation); } } this.second = dt.Second; this.minute = dt.Minute; this.hour = dt.Hour; } } } //一个订阅者,DisplayClock订阅 //时钟事件,DisplayClock任务是显示当前时间 public class DisplayClock { //对于给定的时钟,订阅其他OnSecondChange事件 public void Subscribe(Clock theClock) { theClock.OnSecondChange += new Clock.SecondChangeHandler(TimeHasChanged); } //实现委托功能的方法 public void TimeHasChanged(object clcok, TimeInfoEventArg timeInformation) { Console.WriteLine("Current time: {0}:{1}:{2}", timeInformation.hour.ToString(), timeInformation.minute.ToString(), timeInformation.second.ToString()); } } //另一个订阅者,其任务写入文件 public class LogCurrentTime { public void Subscribe(Clock theClock) { theClock.OnSecondChange += new Clock.SecondChangeHandler(WriteLogEntry); } //此方法应该写入文件 //写到控制台是为了看到对象不保存状态的效果 public void WriteLogEntry( object theClcok, TimeInfoEventArg ti) { Console.WriteLine("logging to file: {0}:{1}:{2}", ti.hour.ToString(), ti.minute.ToString(), ti.second.ToString()); } } class Program { static void Main(string[] args) { Clock theClock = new Clock(); //创建时钟对象,订阅刚刚创建的时钟 DisplayClock dc = new DisplayClock(); dc.Subscribe(theClock); //创建Log对对象 LogCurrentTime lct = new LogCurrentTime(); lct.Subscribe(theClock); theClock.Run(); Console.ReadLine(); } } }
//匿名方法,无需在注册实例委托
public void Subscribe(Clock theClock) { theClock.OnSecondChange += delegate(object clcok, TimeInfoEventArg timeInformation) { Console.WriteLine("Current time: {0}:{1}:{2}", timeInformation.hour.ToString(), timeInformation.minute.ToString(), timeInformation.second.ToString()); }; }
相关文章推荐
- c#调用COM组件
- c#中虚函数的相关使用方法
- 浅析C# web访问mysql数据库-整理归纳总结
- C#实现的阴历阳历互相转化类实例
- C# 的关键字详细介绍
- c# 获得局域网主机列表实例
- 关于C# Math 处理奇进偶不进的实现代码
- c# 随机函数的使用详解
- c#之圆形无标题栏椭圆窗体的实现详解
- C#页面之间跳转功能的小结
- 浅析C# 基础语法的使用
- C#中将字符串转换为整型的三种解决方法总结
- 深入c# GDI+简单绘图的具体操作步骤(一)
- C#中英文混合字符串截取函数
- C# 面向对象的基本原则
- 使用C#开源文件实时监控工具Tail&TailUI介绍
- C# 正则判断一个数字的格式是否有逗号的代码
- C# 参考之访问关键字:base、this
- c#结构和类的相关介绍
- C# 超高面试题收集整理