1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Test
7 {
8 class Start
9 {
10 public static void Main()
11 {
12 Tool.Fan fan = new Tool.Fan();
13 fan.Rotating += new Tool.RotatingEventHander(fan_Rotating);
14 fan.PowerOn();
15 }
16
17 static void fan_Rotating(object sender, Tool.RotatingEventArgs e)
18 {
19 Tool.Fan fan = (Tool.Fan)sender;
20 Console.WriteLine("The brand name of this product: {0}.\nPlace of production: {1}.",fan.BrandName,fan.PlaceOfProduction);
21 Console.Write("\n");
22 Console.WriteLine("This fan is power on now.");
23 Console.WriteLine("Current speed is {0}\nCurrent power is {1}.", e.Speed, e.Power);
24 Console.Write("\n");
25 e.Cancel = true;
26 Console.WriteLine("This fan is power off now.");
27 Console.WriteLine("Current speed is {0}.\nCurrent power is {1}.", e.Speed, e.Power);
28 Console.ReadKey();
29 }
30 }
31 }
32
33 namespace Tool
34 {
35 public class RotatingEventArgs : EventArgs //监视对象转动中的一些数据
36 {
37 private int _speed;//转速
38 public int Speed
39 {
40 get
41 {
42 return _speed;
43 }
44 }
45 private int _power;//功率
46 public int Power
47 {
48 get
49 {
50 return _power;
51 }
52 }
53 private bool _cancel = false;//是否取消该转动
54 public bool Cancel
55 {
56 set
57 {
58 _cancel = value;
59 if (_cancel == true)
60 {
61 _speed = 0;
62 _power = 0;
63 }
64 }
65 get
66 {
67 return _cancel;
68 }
69 }
70 public RotatingEventArgs() //构造函数
71 {
72 _speed = 2300;
73 _power = 5;
74 }
75 }
76
77 //定义委托
78 public delegate void RotatingEventHander(object sender, RotatingEventArgs e);
79 public class Fan //监视对象
80 {
81 //定义事件
82 public event RotatingEventHander Rotating;
83
84 protected virtual void OnRotating(RotatingEventArgs e)
85 {
86 if (Rotating != null)//事件方法注册
87 {
88 Rotating(this, e);
89 }
90 }
91 public void PowerOn()
92 {
93 RotatingEventArgs e = new RotatingEventArgs();
94 OnRotating(e);
95 }
96 public readonly String BrandName = "CNC";
97 public readonly string PlaceOfProduction = "Guangzhou,China";
98 }
99 }