• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Dreama
只想要简简单单的快乐!
博客园    首页    新随笔    联系   管理     
事件、委托的定义与使用示例
 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 }

 

posted on 2014-01-14 19:54  Dreamma  阅读(397)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3

© 本文章版权归 Dreama 所有, 转载授权请联系: cnxy@88.com

如果本文对您有帮助,欢迎支持原创

支付宝

支付宝扫码支持

微信

微信赞赏支持