一定要标明出处,波哥的文章。所有文章都值得一看。这篇是摘抄的大白话之C#事件讲解。委托
http://www.cnblogs.com/wudiwushen/archive/2010/04/20/1703368.html
例子是小明委托小明去买电影票
c#语法如下:
方法有什么区别呢?
方法的地址,而他自己也是什么也不做的。
</span><span style="color: #008000;">//</span><span style="color: #008000;">小明类</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> MrMing
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">声明一个委托,其实就是个“命令”</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">delegate</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> BugTicketEventHandler();
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">这里就是具体阐述这个命令是干什么的,本例是MrZhang.BuyTicket“小张买车票”</span>
BugTicketEventHandler myDelegate = <span style="color: #0000ff;">new</span><span style="color: #000000;"> BugTicketEventHandler(MrZhang.BuyTicket);
</span><span style="color: #008000;">//</span><span style="color: #008000;">这时候委托被附上了具体的<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>
<span style="color: #000000;"> myDelegate();
Console.ReadKey();
}
}
方法,方法);方法必须要加上,因为委托的构造函数是不为空的。
方法要一致,例:
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> BuyMovieTicket()
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">我去,自己泡妞,还要让我带电影票!</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">小明类</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> MrMing
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">声明一个委托,其实就是个“命令”</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">delegate</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> BugTicketEventHandler();
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">这里就是具体阐述这个命令是干什么的,本例是MrZhang.BuyTicket“小张买车票”</span>
BugTicketEventHandler myDelegate = <span style="color: #0000ff;">new</span><span style="color: #000000;"> BugTicketEventHandler(MrZhang.BuyTicket);
myDelegate </span>+=<span style="color: #000000;"> MrZhang.BuyMovieTicket;
</span><span style="color: #008000;">//</span><span style="color: #008000;">这时候委托被附上了具体的<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>
<span style="color: #000000;"> myDelegate();
Console.ReadKey();
}
}
其实,我们只是在程序中加了
好了,关于大白话的委托就介绍到这里了,是不是并没有大家想的这么可怕呢,在下一回我们就接着大白话的EVENT的讲解。
链接 原文出处 波哥 http://www.cnblogs.com/wudiwushen/archive/2010/04/20/1703763.html
事件是类在发生其关注的事情时用来提供通知的一种方式。
事件发行者(Publisher):一个事件的发行者,也称作是发送者(sender),其实就是个对象,这个对象会自行维护本身的状态信息,当本身状态信息变动时,便触发一个事件,并通知说有的事件订阅者。
事件订阅者(Subscriber):对事件感兴趣的对象,也称为Receiver,可以注册感兴趣的事件,在事件发行者触发一个事件后,会自动执行这段代码。
代码:
</span><span style="color: #008000;">//</span><span style="color: #008000;">Subscriber <a href="https://www.jb51.cc/tag/dingyue/" target="_blank" class="keywords">订阅</a>者,无赖小明</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MrMing
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">对事件感兴趣的事情,这里指对出版社的书感兴趣</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Receive()
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">嘎嘎,我已经收到最新一期的《火影忍者》啦!!</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">Subscriber <a href="https://www.jb51.cc/tag/dingyue/" target="_blank" class="keywords">订阅</a>者,悲情人物小张</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MrZhang
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">对事件感兴趣的事情</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Receive()
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">幼稚,这么大了,还看《火影忍者》,SB小明!</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
}
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Story
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">实例化一个出版社</span>
Publisher publisher = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Publisher();
</span><span style="color: #008000;">//</span><span style="color: #008000;">给这个出火影忍者的事件<a href="https://www.jb51.cc/tag/zhuce/" target="_blank" class="keywords">注册</a>感兴趣的<a href="https://www.jb51.cc/tag/dingyue/" target="_blank" class="keywords">订阅</a>者,此例中是小明</span>
publisher.OnPublish += <span style="color: #0000ff;">new</span><span style="color: #000000;"> Publisher.PublishEventHander(MrMing.Receive);
</span><span style="color: #008000;">//</span><span style="color: #008000;">另一种事件<a href="https://www.jb51.cc/tag/zhuce/" target="_blank" class="keywords">注册</a>方式
</span><span style="color: #008000;">//</span><span style="color: #008000;">publisher.OnPublish += MrMing.Receive;
</span><span style="color: #008000;">//</span><span style="color: #008000;">发布者在这里触发出版火影忍者的事件</span>
<span style="color: #000000;"> publisher.issue();
Console.ReadKey();
}
}</span></pre>
结果如下:
订阅者了吧,那至于事件呢
订阅他喜欢看的火影忍者,小张没有订阅所以小张没有收到书,
委托赋值:
内容,我们在下一讲里,我们会讲到我们期盼已久的Sender,e两个神秘的参数了。
链接 http://www.cnblogs.com/wudiwushen/archive/2010/04/21/1717378.html
今天我接着上面的3篇文章来讲一下,为什么我们在日常的编程活动中遇到这么多sender,EventArgs e 参数:
protected void Page_Load(object sender,EventArgs e)
{
}
protected void btnSearch_Click(object sender,ImageClickEventArgs e)
{
}
protected void grdBill_RowDataBound(object sender,GridViewRowEventArgs e)
{
}
那他们到底表示什么呢?
在回答上面的问题之前,我们先搞懂 .Net Framework的编码规范:
一、委托类型的名称都应该以EventHandler结束。
二、委托的原型定义:有一个void返回值,并接受两个输入参数:一个Object 类型,一个 EventArgs类型(或继承自EventArgs)。
三、事件的命名为 委托去掉 EventHandler之后剩余的部分。
四、继承自EventArgs的类型应该以EventArgs结尾。
这就是微软编码的规范,当然这不仅仅是规则,而是在这种规则下使程序有更大的灵活性,那我们就继续重构第三讲的例子,让他符合微软的规范。
}
</span><span style="color: #0000ff;">public</span> PubEventArgs (<span style="color: #0000ff;">string</span><span style="color: #000000;"> magazineName)
{
</span><span style="color: #0000ff;">this</span>.magazineName =<span style="color: #000000;"> magazineName;
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">发布者(Publiser)</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Publisher
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">声明一个出版的委托
</span><span style="color: #008000;">//</span><span style="color: #008000;">这里多了一个参数sender,它所代表的就是Subject,也就是监视对象,本例中就是Publisher</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">delegate</span> <span style="color: #0000ff;">void</span> PublishEventHander(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender,PubEventArgs e);
</span><span style="color: #008000;">//</span><span style="color: #008000;">在委托的机制下我们建立以个出版事件</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">event</span><span style="color: #000000;"> PublishEventHander Publish;
</span><span style="color: #008000;">//</span><span style="color: #008000;">声明一个可重写的OnPublish的保护<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a></span>
<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnPublish(PubEventArgs e)
{
</span><span style="color: #0000ff;">if</span> (Publish != <span style="color: #0000ff;">null</span><span style="color: #000000;">)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">Sender = this,也就是Publisher</span>
<span style="color: #0000ff;">this</span>.Publish(<span style="color: #0000ff;">this</span><span style="color: #000000;">,e);
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">事件必须要在<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>里去触发</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> issue(<span style="color: #0000ff;">string</span><span style="color: #000000;"> magazineName)
{
OnPublish(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> PubEventArgs(magazineName));
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">Subscriber <a href="https://www.jb51.cc/tag/dingyue/" target="_blank" class="keywords">订阅</a>者</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MrMing
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">对事件感兴趣的事情</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Receive(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender,PubEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">嘎嘎,我已经收到最新一期的《</span><span style="color: #800000;">"</span>+e.magazineName+<span style="color: #800000;">"</span><span style="color: #800000;">》啦!!</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MrZhang
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">对事件感兴趣的事情</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Receive(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender,PubEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">幼稚,这么大了,还看《火影忍者》,SB小明!</span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">这个我定的《</span><span style="color: #800000;">"</span>+e.magazineName+<span style="color: #800000;">"</span><span style="color: #800000;">》,哇哈哈!</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
}
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Story
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">实例化一个出版社</span>
Publisher publisher = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Publisher();
Console.Write(</span><span style="color: #800000;">"</span><span style="color: #800000;">请输入要发行的杂志:</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">string</span> name =<span style="color: #000000;"> Console.ReadLine();
</span><span style="color: #0000ff;">if</span> (name == <span style="color: #800000;">"</span><span style="color: #800000;">火影忍者</span><span style="color: #800000;">"</span><span style="color: #000000;">)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">给这个出火影忍者的事件<a href="https://www.jb51.cc/tag/zhuce/" target="_blank" class="keywords">注册</a>感兴趣的<a href="https://www.jb51.cc/tag/dingyue/" target="_blank" class="keywords">订阅</a>者,此例中是小明</span>
publisher.Publish += <span style="color: #0000ff;">new</span><span style="color: #000000;"> Publisher.PublishEventHander(MrMing.Receive);
</span><span style="color: #008000;">//</span><span style="color: #008000;">发布者在这里触发出版火影忍者的事件</span>
publisher.issue(<span style="color: #800000;">"</span><span style="color: #800000;">火影忍者</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">给这个出火影忍者的事件<a href="https://www.jb51.cc/tag/zhuce/" target="_blank" class="keywords">注册</a>感兴趣的<a href="https://www.jb51.cc/tag/dingyue/" target="_blank" class="keywords">订阅</a>者,此例中是小明[另一种事件<a href="https://www.jb51.cc/tag/zhuce/" target="_blank" class="keywords">注册</a>方式]</span>
publisher.Publish +=<span style="color: #000000;"> MrZhang.Receive;
publisher.issue(</span><span style="color: #800000;">"</span><span style="color: #800000;">环球日报</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
Console.ReadKey();
}
}</span></pre>
订阅的事件
显示
通过例子我再做一次说明,其实我们不用把Sender,e想的过于可怕
一、委托声明原型中的Object类型的参数代表了Subject,也就是监视对象,在本例中是 Publisher(出版社)。。
二、EventArgs 对象包含了Observer所感兴趣的数据,在本例中是杂志。
好了,我们接着讲我们的委托与事件,其实如果大家对设计模式精通的话,其实他们关联的是观察者(Observer)模式,这里我就不再描述什么是观察者模式了,只是简单讲一下他们的关联:
如果看不懂的话也没关系,当大家OO达到一定程度了,自然而然就会明白。
最后我们来看一个我们日常最最常用的观察者模式:
场景:当我们用信用卡刷完钱的时候,我们就会接收到手机短信,或者是电子邮件,其实这就是Observer pattern
</span><span style="color: #008000;">//</span><span style="color: #008000;">Obverser电子<a href="https://www.jb51.cc/tag/youjian/" target="_blank" class="keywords">邮件</a>,手机关心的对象e,分别是<a href="https://www.jb51.cc/tag/youjian/" target="_blank" class="keywords">邮件</a>地址、手机号码、取款金额</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> UserEventArgs : EventArgs
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">readonly</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> emailAddress;
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">readonly</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> mobilePhone;
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">readonly</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> amount;
</span><span style="color: #0000ff;">public</span> UserEventArgs(<span style="color: #0000ff;">string</span> emailAddress,<span style="color: #0000ff;">string</span> mobilePhone,<span style="color: #0000ff;">string</span><span style="color: #000000;"> amount)
{
</span><span style="color: #0000ff;">this</span>.emailAddress =<span style="color: #000000;"> emailAddress;
</span><span style="color: #0000ff;">this</span>.mobilePhone =<span style="color: #000000;"> mobilePhone;
</span><span style="color: #0000ff;">this</span>.amount =<span style="color: #000000;"> amount;
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">发布者,也就是被监视的对象-银行账号</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> BankAccount
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">声明一个处理银行交易的委托</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">delegate</span> <span style="color: #0000ff;">void</span> ProcessTranEventHandler(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender,UserEventArgs e);
</span><span style="color: #008000;">//</span><span style="color: #008000;">声明一个事件</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">event</span><span style="color: #000000;"> ProcessTranEventHandler ProcessTran;
</span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnProcessTran(UserEventArgs e)
{
</span><span style="color: #0000ff;">if</span> (ProcessTran != <span style="color: #0000ff;">null</span><span style="color: #000000;">)
{
ProcessTran(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">,e);
}
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> Prcess(UserEventArgs e)
{
OnProcessTran(e);
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">观察者Email</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Email
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> SendEmail(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender,UserEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">向<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>邮箱</span><span style="color: #800000;">"</span> + e.emailAddress + <span style="color: #800000;">"</span><span style="color: #800000;"><a href="https://www.jb51.cc/tag/fasongyoujian/" target="_blank" class="keywords">发送邮件</a>:您在</span><span style="color: #800000;">"</span>+System.DateTime.Now.ToString()+<span style="color: #800000;">"</span><span style="color: #800000;">取款金额为</span><span style="color: #800000;">"</span>+<span style="color: #000000;">e.amount);
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">观察者手机</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Mobile
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> SendNotification(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender,UserEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">向<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>手机</span><span style="color: #800000;">"</span> + e.mobilePhone + <span style="color: #800000;">"</span><span style="color: #800000;">发送短信:您在</span><span style="color: #800000;">"</span> + System.DateTime.Now.ToString() + <span style="color: #800000;">"</span><span style="color: #800000;">取款金额为</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.amount);
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;"><a href="https://www.jb51.cc/tag/dingyue/" target="_blank" class="keywords">订阅</a>系统,实现银行系统<a href="https://www.jb51.cc/tag/dingyue/" target="_blank" class="keywords">订阅</a>几个Observer,实现与客户端的松耦合</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> SubscribSystem
{
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> SubscribSystem()
{
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> SubscribSystem(BankAccount bankAccount,UserEventArgs e)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">现在我们在银行账户<a href="https://www.jb51.cc/tag/dingyue/" target="_blank" class="keywords">订阅</a>2个,分别是电子<a href="https://www.jb51.cc/tag/youjian/" target="_blank" class="keywords">邮件</a>和手机短信</span>
bankAccount.ProcessTran += <span style="color: #0000ff;">new</span><span style="color: #000000;"> BankAccount.ProcessTranEventHandler(Email.SendEmail);
bankAccount.ProcessTran </span>+= <span style="color: #0000ff;">new</span><span style="color: #000000;"> BankAccount.ProcessTranEventHandler(Mobile.SendNotification);
bankAccount.Prcess(e);
}
}
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Client
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
{
Console.Write(</span><span style="color: #800000;">"</span><span style="color: #800000;">请输入您要取款的金额:</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">string</span> amount =<span style="color: #000000;"> Console.ReadLine();
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">交易成功,请取磁卡。</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">初始化e</span>
UserEventArgs user = <span style="color: #0000ff;">new</span> UserEventArgs(<span style="color: #800000;">"</span><span style="color: #800000;">jinjiangbo2008@163.com</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">18868789776</span><span style="color: #800000;">"</span><span style="color: #000000;">,amount);
</span><span style="color: #008000;">//</span><span style="color: #008000;">初始化<a href="https://www.jb51.cc/tag/dingyue/" target="_blank" class="keywords">订阅</a>系统</span>
SubscribSystem subject = <span style="color: #0000ff;">new</span> SubscribSystem(<span style="color: #0000ff;">new</span><span style="color: #000000;"> BankAccount(),user);
Console.ReadKey();
}
}</span></pre>
运行结果如下:
网上还有个热水器烧水的OBSERVER PATTERN 也是蛮经典的,大家可以看看。
原文链接:https://www.f2er.com/csharp/238758.html