|
|
如果你可以很清楚明白上面两部分代码是什么意思,说明你已经很了解Delegate(至少比我了解 ), |
| 所以我想下面的文章也许对你的帮助不是很大. 不过如果某些地方你也有所疑惑,希望你能从我下面的文章中得到一些帮助. |
|
|
 |
|
| 这篇文章都包含哪些内容 |
|
| 这篇文章从最基本的Delegate开始介绍起,后续会引申到event, 匿名Delegate 及我个人对其使用的一点理解 |
|
|
| 提前需要阅读的文章 |
|
| 如果你像我当初一样对Delegate完全不理解,请先阅读NET之美:.NET关键技术深入解析 迷你书 中的 第3章 (C# 中的委托和事件). |
(作者对Delegate理解的及其深入,我是比不上了 ) |
|
|
 |
|
| 什么是 Delegate |
|
| 从观察者模式说起 |
|
| 观察者模式大家肯定都不陌生 |
| [C#] |
| using System; |
| using System.Collections; |
| namespace L1 |
| { |
| public class FakeBloodChangeDelegate |
| { |
| public ArrayList mAllFakeDelegateListener; |
|
| public FakeBloodChangeDelegate () |
| { |
| mAllFakeDelegateListener = new ArrayList (); |
| } |
|
| public void addListener (IFakeBloodNumChangeDelegateListener _listener) |
| { |
| mAllFakeDelegateListener.Add (_listener); |
| } |
|
| public void upldateListener () |
| { |
| foreach (IFakeBloodNumChangeDelegateListener listener in mAllFakeDelegateListener) |
| { |
| listener.onBloodNumChange (); |
| } |
| } |
| } |
| } |
|
|
| [C#] |
| using System; |
| namespace L1 |
| { |
| public interface IFakeBloodNumChangeDelegateListener |
| { |
| void onBloodNumChange (); |
| } |
| } |
|
|
[C#] using System;
|
namespace L1
|
{
|
public class Idiot : IFakeBloodNumChangeDelegateListener
|
{
|
public Idiot ()
|
{
|
}
|
|
public void onBloodNumChange ()
|
{
|
Console.WriteLine ("Idiot --> onBloodNumChange");
|
}
|
}
|
}
|
|
|
|
|
| [C#] |