必须声明事件的委派类。如
public delegate void MeltdownHandler (
object reactor,
MeltdownEventArgs myMEA
);
全部的事件处理器委派都必须返回void并接受两个参数。第一个参数为对象,它代表产生事件的对象--在下面例子中,表示可能熔化的反应堆。
第二个参数是从一个System.EventArgs类派生而来的类的对象。EventArgs类是事件数据的基类,并代表了事件的细节。在这个例子中,为:
public class MeltdownEventArgs : EventArgs
{
// declare a private field named message
private string message;
// define a constructor
public MeltdownEventArgs(string message)
{
this.message = message;
}
// define a property to get the message
public string Message
{
get
{
return message;
}
}
}
声明Reactor类
下一步声明一个类来代表反应堆,设这个类为Reactor.Reactor类包含OnMeltdown事件和MeltdownHandler委派类的声明。Reactor类声明:
// declare the Reactor class
public class Reactor
{
// declare a private field named temperature
private int temperature;
// declare a delegate class named MeltdownHandler
public delegate void MeltdownHandler(
object reactor,
MeltdownEventArgs myMEA
);
// declare an event named OnMeltdown
public event MeltdownHandler OnMeltdown;
// define a property to set the temperature
public int Temperature
{
set
{
temperature = value;
// if the temperature is too high, the reactor melts down
if (temperature > 1000)
{
MeltdownEventArgs myMEA =
new MeltdownEventArgs("Reactor meltdown in progress!");
OnMeltdown(this, myMEA);
}
}
}
}
还可声明一个ReactorMonitor类监视Reactor类,如下:
// declare the ReactorMonitor class
public class ReactorMonitor
{
// define a constructor
public ReactorMonitor(Reactor myReactor)
{
myReactor.OnMeltdown +=
new Reactor.MeltdownHandler(DisplayMessage);
}
// define the DisplayMessage() method
public void DisplayMessage(
object myReactor, MeltdownEventArgs myMEA
)
{
Console.WriteLine(myMEA.Message);
}
}
全部代码为:
1 /*
2 Example12_4.cs illustrates the use of an event
3 */
4
5 using System;
6
7
8 // declare the MeltdownEventArgs class (implements EventArgs)
9 public class MeltdownEventArgs : EventArgs
10 {
11
12 // declare a private field named message
13 private string message;
14
15 // define a constructor
16 public MeltdownEventArgs(string message)
17 {
18 this.message = message;
19 }
20
21 // define a property to get the message
22 public string Message
23 {
24 get
25 {
26 return message;
27 }
28 }
29
30 }
31
32
33 // declare the Reactor class
34 public class Reactor
35 {
36
37 // declare a private field named temperature
38 private int temperature;
39
40 // declare a delegate class named MeltdownHandler
41 public delegate void MeltdownHandler(
42 object reactor,
43 MeltdownEventArgs myMEA
44 );
45
46 // declare an event named OnMeltdown
47 public event MeltdownHandler OnMeltdown;
48
49 // define a property to set the temperature
50 public int Temperature
51 {
52 set
53 {
54 temperature = value;
55
56 // if the temperature is too high, the reactor melts down
57 if (temperature > 1000)
58 {
59 MeltdownEventArgs myMEA =
60 new MeltdownEventArgs("Reactor meltdown in progress!");
61 OnMeltdown(this, myMEA);
62 }
63 }
64 }
65
66 }
67
68
69 // declare the ReactorMonitor class
70 public class ReactorMonitor
71 {
72
73 // define a constructor
74 public ReactorMonitor(Reactor myReactor)
75 {
76 myReactor.OnMeltdown +=
77 new Reactor.MeltdownHandler(DisplayMessage);
78 }
79
80 // define the DisplayMessage() method
81 public void DisplayMessage(
82 object myReactor, MeltdownEventArgs myMEA
83 )
84 {
85 Console.WriteLine(myMEA.Message);
86 }
87
88 }
89
90
91 class Example12_5
92 {
93
94 public static void Main()
95 {
96
97 // create a Reactor object
98 Reactor myReactor = new Reactor();
99
100 // create a ReactorMonitor object
101 ReactorMonitor myReactorMonitor = new ReactorMonitor(myReactor);
102
103 // set myReactor.Temperature to 100 degrees Centigrade
104 Console.WriteLine("Setting reactor temperature to 100 degrees Centigrade");
105 myReactor.Temperature = 100;
106
107 // set myReactor.Temperature to 500 degrees Centigrade
108 Console.WriteLine("Setting reactor temperature to 500 degrees Centigrade");
109 myReactor.Temperature = 500;
110
111 // set myReactor.Temperature to 2000 degrees Centigrade
112 // (this causes the reactor to meltdown)
113 Console.WriteLine("Setting reactor temperature to 2000 degrees Centigrade");
114 myReactor.Temperature = 2000;
115
116 }
117
118 }