标签:
1
2
3
4
5
6
7
8
9
|
public void GreetPeople( string name) { // 做某些额外的事情,比如初始化之类,此处略 EnglishGreeting(name); } public void EnglishGreeting( string name) { Console.WriteLine( "Morning, " + name); } |
1
2
3
4
|
public void ChineseGreeting( string name) { Console.WriteLine( "早上好, " + name); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public enum Language { English, Chinese } public void GreetPeople( string name, Language lang) { //做某些额外的事情,比如初始化之类,此处略 switch (lang) { case Language.English: EnglishGreeting(name); break ; case Language.Chinese: ChineseGreeting(name); break ; } } |
1
2
3
4
|
public void GreetPeople( string name, *** MakeGreeting) { MakeGreeting(name); } |
1
2
|
public void EnglishGreeting( string name) public void ChineseGreeting( string name) |
1
2
3
4
|
public void GreetPeople( string name, GreetingDelegate MakeGreeting) { MakeGreeting(name); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
using System; using System.Collections.Generic; using System.Text; namespace Delegate { //定义委托,它定义了可以代表的方法的类型 public delegate void GreetingDelegate( string name); class Program { private static void EnglishGreeting( string name) { Console.WriteLine( "Morning, " + name); } private static void ChineseGreeting( string name) { Console.WriteLine( "早上好, " + name); } //注意此方法,它接受一个GreetingDelegate类型的参数,该参数是返回值为空,参数为string类型的方法 private static void GreetPeople( string name, GreetingDelegate MakeGreeting) { MakeGreeting(name); } static void Main( string [] args) { GreetPeople( "Jimmy Zhang" , EnglishGreeting); GreetPeople( "张子阳" , ChineseGreeting); Console.ReadKey(); } } } |
1
2
3
4
5
6
7
8
|
static void Main( string [] args) { string name1, name2; name1 = "Jimmy Zhang" ; name2 = "张子阳" ; GreetPeople(name1, EnglishGreeting); GreetPeople(name2, ChineseGreeting); Console.ReadKey(); } |
1
2
3
4
5
6
7
8
|
static void Main( string [] args) { GreetingDelegate delegate1, delegate2; delegate1 = EnglishGreeting; delegate2 = ChineseGreeting; GreetPeople( "Jimmy Zhang" , delegate1); GreetPeople( "张子阳" , delegate2); Console.ReadKey(); } |
1
2
3
4
5
6
7
8
|
static void Main( string [] args) { GreetingDelegate delegate1; delegate1 = EnglishGreeting; // 先给委托类型的变量赋值 delegate1 += ChineseGreeting; // 给此委托变量再绑定一个方法 // 将先后调用 EnglishGreeting 与 ChineseGreeting 方法 GreetPeople( "Jimmy Zhang" , delegate1); Console.ReadKey(); } |
1
2
3
4
5
6
7
8
|
static void Main( string [] args) { GreetingDelegate delegate1; delegate1 = EnglishGreeting; // 先给委托类型的变量赋值 delegate1 += ChineseGreeting; // 给此委托变量再绑定一个方法 // 将先后调用 EnglishGreeting 与 ChineseGreeting 方法 delegate1 ( "Jimmy Zhang" ); Console.ReadKey(); } |
1
2
3
4
5
6
7
8
9
10
11
|
static void Main( string [] args) { GreetingDelegate delegate1 = new GreetingDelegate(EnglishGreeting); delegate1 += ChineseGreeting; // 给此委托变量再绑定一个方法 // 将先后调用 EnglishGreeting 与 ChineseGreeting 方法 GreetPeople( "Jimmy Zhang" , delegate1); Console.WriteLine(); delegate1 -= EnglishGreeting; //取消对EnglishGreeting方法的绑定 // 将仅调用 ChineseGreeting GreetPeople( "张子阳" , delegate1); Console.ReadKey(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
namespace Delegate { //定义委托,它定义了可以代表的方法的类型 public delegate void GreetingDelegate( string name); //新建的GreetingManager类 public class GreetingManager{ public void GreetPeople( string name, GreetingDelegate MakeGreeting) { MakeGreeting(name); } } class Program { private static void EnglishGreeting( string name) { Console.WriteLine( "Morning, " + name); } private static void ChineseGreeting( string name) { Console.WriteLine( "早上好, " + name); } static void Main( string [] args) { // ... ... } } } |
1
2
3
4
5
|
static void Main( string [] args) { GreetingManager gm = new GreetingManager(); gm.GreetPeople( "Jimmy Zhang" , EnglishGreeting); gm.GreetPeople( "张子阳" , ChineseGreeting); } |
1
2
3
4
5
6
7
|
static void Main( string [] args) { GreetingManager gm = new GreetingManager(); GreetingDelegate delegate1; delegate1 = EnglishGreeting; delegate1 += ChineseGreeting; gm.GreetPeople( "Jimmy Zhang" , delegate1); } |
1
2
3
4
5
6
7
|
public class GreetingManager{ //在GreetingManager类的内部声明delegate1变量 public GreetingDelegate delegate1; public void GreetPeople( string name, GreetingDelegate MakeGreeting) { MakeGreeting(name); } } |
1
2
3
4
5
6
|
static void Main( string [] args) { GreetingManager gm = new GreetingManager(); gm.delegate1 = EnglishGreeting; gm.delegate1 += ChineseGreeting; gm.GreetPeople( "Jimmy Zhang" , gm.delegate1); } |
1
2
3
4
5
6
7
8
9
|
public class GreetingManager{ //在GreetingManager类的内部声明delegate1变量 public GreetingDelegate delegate1; public void GreetPeople( string name) { if (delegate1!= null ){ //如果有方法注册委托变量 delegate1(name); //通过委托调用方法 } } } |
1
2
3
4
5
6
|
static void Main( string [] args) { GreetingManager gm = new GreetingManager(); gm.delegate1 = EnglishGreeting; gm.delegate1 += ChineseGreeting; gm.GreetPeople( "Jimmy Zhang" ); //注意,这次不需要再传递 delegate1变量 } |
1
2
3
4
5
6
7
|
public class GreetingManager{ //这一次我们在这里声明一个事件 public event GreetingDelegate MakeGreet; public void GreetPeople( string name) { MakeGreet(name); } } |
1
2
3
4
5
6
|
static void Main( string [] args) { GreetingManager gm = new GreetingManager(); gm.MakeGreet = EnglishGreeting; // 编译错误1 gm.MakeGreet += ChineseGreeting; gm.GreetPeople( "Jimmy Zhang" ); } |
1
2
3
4
5
6
7
8
9
|
private GreetingDelegate MakeGreet; //对事件的声明 实际是 声明一个私有的委托变量 [MethodImpl(MethodImplOptions.Synchronized)] public void add_MakeGreet(GreetingDelegate value){ this .MakeGreet = (GreetingDelegate) Delegate.Combine( this .MakeGreet, value); } [MethodImpl(MethodImplOptions.Synchronized)] public void remove_MakeGreet(GreetingDelegate value){ this .MakeGreet = (GreetingDelegate) Delegate.Remove( this .MakeGreet, value); } |
1
2
3
4
5
6
7
8
|
public delegate void GreetingDelegate( string name); 当编译器遇到这段代码的时候,会生成下面这样一个完整的类: public sealed class GreetingDelegate:System.MulticastDelegate{ public GreetingDelegate( object @ object , IntPtr method); public virtual IAsyncResult BeginInvoke( string name, AsyncCallback callback, object @ object ); public virtual void EndInvoke(IAsyncResult result); public virtual void Invoke( string name); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
using System; using System.Collections.Generic; using System.Text; namespace Delegate { // 热水器 public class Heater { private int temperature; public delegate void BoilHandler( int param); //声明委托 public event BoilHandler BoilEvent; //声明事件 // 烧水 public void BoilWater() { for ( int i = 0; i <= 100; i++) { temperature = i; if (temperature > 95) { if (BoilEvent != null ) { //如果有对象注册 BoilEvent(temperature); //调用所有注册对象的方法 } } } } } // 警报器 public class Alarm { public void MakeAlert( int param) { Console.WriteLine( "Alarm:嘀嘀嘀,水已经 {0} 度了:" , param); } } // 显示器 public class Display { public static void ShowMsg( int param) { //静态方法 Console.WriteLine( "Display:水快烧开了,当前温度:{0}度。" , param); } } class Program { static void Main() { Heater heater = new Heater(); Alarm alarm = new Alarm(); heater.BoilEvent += alarm.MakeAlert; //注册方法 heater.BoilEvent += ( new Alarm()).MakeAlert; //给匿名对象注册方法 heater.BoilEvent += Display.ShowMsg; //注册静态方法 heater.BoilWater(); //烧水,会自动调用注册过对象的方法 } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
namespace Delegate { class Heater { private int temperature; // 水温 // 烧水 public void BoilWater() { for ( int i = 0; i <= 100; i++) { temperature = i; if (temperature > 95) { MakeAlert(temperature); ShowMsg(temperature); } } } // 发出语音警报 private void MakeAlert( int param) { Console.WriteLine( "Alarm:嘀嘀嘀,水已经 {0} 度了:" , param); } // 显示水温 private void ShowMsg( int param) { Console.WriteLine( "Display:水快开了,当前温度:{0}度。" , param); } } class Program { static void Main() { Heater ht = new Heater(); ht.BoilWater(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
// 热水器 public class Heater { private int temperature; // 烧水 private void BoilWater() { for ( int i = 0; i <= 100; i++) { temperature = i; } } } // 警报器 public class Alarm{ private void MakeAlert( int param) { Console.WriteLine( "Alarm:嘀嘀嘀,水已经 {0} 度了:" , param); } } // 显示器 public class Display{ private void ShowMsg( int param) { Console.WriteLine( "Display:水已烧开,当前温度:{0}度。" , param); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System; using System.Collections.Generic; using System.Text; namespace Delegate { // 热水器 public class Heater { private int temperature; public string type = "RealFire 001" ; // 添加型号作为演示 public string area = "China Xi‘an" ; // 添加产地作为演示 //声明委托 public delegate void BoiledEventHandler(Object sender, BoiledEventArgs e); public event BoiledEventHandler Boiled; //声明事件 // 定义BoiledEventArgs类,传递给Observer所感兴趣的信息 //继承EventArgs public class BoiledEventArgs : EventArgs { public readonly int temperature; // public BoiledEventArgs( int temperature) { this .temperature = temperature; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
// 可以供继承自 Heater 的类重写,以便继承类拒绝其他对象对它的监视 protected virtual void OnBoiled(BoiledEventArgs e) { if (Boiled != null ) { // 如果有对象注册 Boiled( this , e); // 调用所有注册对象的方法 } } // 烧水。 public void BoilWater() { for ( int i = 0; i <= 100; i++) { temperature = i; if (temperature > 95) { //建立BoiledEventArgs 对象。 BoiledEventArgs e = new BoiledEventArgs(temperature); OnBoiled(e); // 调用 OnBoiled方法 } } } } // 警报器 public class Alarm { public void MakeAlert(Object sender, Heater.BoiledEventArgs e) { Heater heater = (Heater)sender; //这里是不是很熟悉呢? //访问 sender 中的公共字段 Console.WriteLine( "Alarm:{0} - {1}: " , heater.area, heater.type); Console.WriteLine( "Alarm: 嘀嘀嘀,水已经 {0} 度了:" , e.temperature); Console.WriteLine(); } } // 显示器 public class Display { public static void ShowMsg(Object sender, Heater.BoiledEventArgs e) { //静态方法 Heater heater = (Heater)sender; Console.WriteLine( "Display:{0} - {1}: " , heater.area, heater.type); Console.WriteLine( "Display:水快烧开了,当前温度:{0}度。" , e.temperature); Console.WriteLine(); } } class Program { static void Main() { Heater heater = new Heater(); Alarm alarm = new Alarm(); heater.Boiled += alarm.MakeAlert; //注册方法 heater.Boiled += ( new Alarm()).MakeAlert; //给匿名对象注册方法 heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert); //也可以这么注册 heater.Boiled += Display.ShowMsg; //注册静态方法 heater.BoilWater(); //烧水,会自动调用注册过对象的方法 } } } |
标签:
原文地址:http://www.cnblogs.com/sdya/p/4543580.html