转载:委托和事件
2008-11-16 22:16
204 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication7
{
delegate void 事件处理格式1();
delegate void 事件处理格式2(int n);
class 遥控器 //事件引发者
{
public event 事件处理格式1 事件之按下开键; //遥控器上的开关
public event 事件处理格式1 事件之按下关键;
public event 事件处理格式2 事件之按下数字键;//遥控器上的选台键
public void 开机()
{
if (事件之按下开键 != null)
事件之按下开键();
//如果事件有响应者,发送事件给响应者
}
public void 关机()
{
if(事件之按下关键 != null)
事件之按下关键();
}
public void 选台(int 频道)
{
if (事件之按下数字键 != null)
事件之按下数字键(频道);
}
}
class 电视机 //事件响应者
{
public void 开机()
{
Console.WriteLine("屏幕亮了");
}
public void 关机()
{
Console.WriteLine("屏幕熄了");
}
public void 转换频道(int n)
{
Console.WriteLine("频道{0}播放",n);
}
}
class 程序运行环境 //用于触发事件
{
static void Main(string[] args)
{
遥控器 我的遥控器 = new 遥控器();
电视机 我的电视机 = new 电视机();
//关联"引发者"和"响应者"
我的遥控器.事件之按下开键 += new 事件处理格式1(我的电视机.开机);
我的遥控器.事件之按下关键 += new 事件处理格式1(我的电视机.关机);
我的遥控器.事件之按下数字键 += new 事件处理格式2(我的电视机.转换频道);
//开始触发事件了
我的遥控器.开机();
我的遥控器.选台(20);
我的遥控器.选台(10);
我的遥控器.选台(5);
我的遥控器.关机();
}
}
}
转自:patrickpan的专栏 http://blog.csdn.net/patrickpan/archive/2008/11/13/3291163.aspx
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication7
{
delegate void 事件处理格式1();
delegate void 事件处理格式2(int n);
class 遥控器 //事件引发者
{
public event 事件处理格式1 事件之按下开键; //遥控器上的开关
public event 事件处理格式1 事件之按下关键;
public event 事件处理格式2 事件之按下数字键;//遥控器上的选台键
public void 开机()
{
if (事件之按下开键 != null)
事件之按下开键();
//如果事件有响应者,发送事件给响应者
}
public void 关机()
{
if(事件之按下关键 != null)
事件之按下关键();
}
public void 选台(int 频道)
{
if (事件之按下数字键 != null)
事件之按下数字键(频道);
}
}
class 电视机 //事件响应者
{
public void 开机()
{
Console.WriteLine("屏幕亮了");
}
public void 关机()
{
Console.WriteLine("屏幕熄了");
}
public void 转换频道(int n)
{
Console.WriteLine("频道{0}播放",n);
}
}
class 程序运行环境 //用于触发事件
{
static void Main(string[] args)
{
遥控器 我的遥控器 = new 遥控器();
电视机 我的电视机 = new 电视机();
//关联"引发者"和"响应者"
我的遥控器.事件之按下开键 += new 事件处理格式1(我的电视机.开机);
我的遥控器.事件之按下关键 += new 事件处理格式1(我的电视机.关机);
我的遥控器.事件之按下数字键 += new 事件处理格式2(我的电视机.转换频道);
//开始触发事件了
我的遥控器.开机();
我的遥控器.选台(20);
我的遥控器.选台(10);
我的遥控器.选台(5);
我的遥控器.关机();
}
}
}
转自:patrickpan的专栏 http://blog.csdn.net/patrickpan/archive/2008/11/13/3291163.aspx
相关文章推荐
- C# 中的委托和事件【转载】
- 【转载】C# 中的委托和事件(详解:简单易懂的讲解)
- 【转载】C# 中的委托和事件
- C#中的委托和事件(转载学习)
- <转载>大白话系列之C#委托与事件讲解(序言)
- C#中的委托和事件(转载)
- C# 中的委托和事件(转载)
- .Net Framework中的委托与事件-转载
- C# 中的委托和事件1(转载)
- 委托、事件、接口等之间的关系和区别[转载]
- C# 中的委托和事件 (转载)
- [翻译][转载]简单谈谈事件与委托
- C# 中的委托和事件(转载)
- 委托和事件(转载)
- .net的事件与委托(转载)
- C#里的委托和事件实现Observer (转载)
- <转载>大白话系列之C#委托与事件讲解(一)
- C# 中的委托和事件 --转载
- C# 中的委托和事件(转载)
- 转载:C#中事件和委托的编译代码