前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >事件、委托的定义与使用示例

事件、委托的定义与使用示例

作者头像
CNXY
发布2017-12-25 14:57:32
7780
发布2017-12-25 14:57:32
举报
文章被收录于专栏:C# 编程C# 编程
代码语言:javascript
复制
 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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-01-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档