摘要:
本文用一个动态变化的clock,模拟时钟的数据变化,即每一次事件触发,都会发送给subscriber不同的值。namespace EventKeyWord{ /// <summary> /// Summary description for Class1. /// </summary> class Test { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static... 阅读全文
摘要:
一般.NET中的事件申明格式都是:public delegate void PlayGameHandler(object sender, CustomEventArgs e); sender表示事件发起者,e表示事件需要传输入的一些参数.本文主要讲述一个事件:Zhang在上班中玩游戏(事件),Wang知道了,然后扣张的钱,最后张的钱被扣了.主要代码逻辑如下:namespace DelegateEventDemo{ class Class1 { static void Main(string[] args) { Console.Wri... 阅读全文
摘要:
本文用Mouse,Cat,Host的例子,讲解设计模式中非常著名的观察者模式:Cat叫的时候, Mouse开始Escape, Host惊醒。这是观察者和动作发起者: public delegate void ShoutDelegate(string str); public class Cat { public event ShoutDelegate ShoutEvent; public void OnShout(string str) { if (ShoutEvent!=null) { ... 阅读全文
摘要:
开篇:.NET中的Delegate和Event是使用最为频繁的关键字之一,在程序设计中,非常方便的可以实现特定的功能,基于学习的目的,本系列特地总结几篇博文,说明Delegate和Event的异同。1 观察者模式(Mouse, Cat, Host)2 上班玩游戏--事件模式3 Clock 动态模式4 用方法限制事件只能被单个订阅者订阅5 用事件访问器限制只能被单个订阅者订阅下面是Delegate和Event的一些异同:1) 事件可以定义在Struct, Class, Interface里, delegate可以放在类里面进行申明(不能放在Interface里面)也可以放在整个命名空间中进行申明 阅读全文