class Heater
{
public int temperature;
//烧水
public void BoilWater()
{
for (int i = 0; i <= 100; i++)
temperature++;
if (temperature > 95)
{
//这里添加我们水烧开的一些操作
}
}
//警报
public void Alarm(int param)
{
WriteLine("滴滴滴。。水已经{0}度",param);
}
//显示
public void Display(int param)
{
WriteLine("水烧开了。。温度是{0}", param);
}
}
namespace Event_Delegate
{
//定义一个委托,这个委托的参数和我们要调用的方法的参数类型要保持一致
public delegate void OperateDelegate(int param);
class Heater
{
//烧水
public void BoilWater(OperateDelegate Operation)
{
int temperature =0;
for (int i = 0; i <= 100; i++)
temperature=i;
if (temperature > 95)
{
if(Operation!=null)
Operation(temperature); //执行烧开后的操作
}
}
//警报
public void Alarm(int param)
{
WriteLine("滴滴滴。。水已经{0}度",param);
}
//显示
public void Display(int param)
{
WriteLine("水烧开了。。温度是{0}", param);
}
}
class Program
{
static void Main(string[] args)
{
Heater heater = new Heater();
OperateDelegate operadelegate;
operadelegate = heater.Display;//将方法绑定到委托
operadelegate += heater.Alarm;
heater.BoilWater(operadelegate);
ReadKey();
}
}
}
static void Main(string[] args)
{
Heater heater = new Heater();
OperateDelegate operadelegate;//定义一个委托类型的变量
operadelegate = heater.Display;//将方法绑定到委托
operadelegate += heater.Alarm;
heater.BoilWater(operadelegate);
ReadKey();
}
namespace Event_Delegate
{
class Heater
{
//定义一个委托,这个委托的参数和我们要调用的方法的参数类型要保持一致
public delegate void OperateDelegate(int param);
//
public event OperateDelegate Operation; //定义了一个事件
//烧水
public void BoilWater()
{
int temperature =0;
for (int i = 0; i <= 100; i++)
temperature=i;
if (temperature > 95)
{
if(Operation!=null)
Operation(temperature); //执行烧开后的操作
}
}
//警报
public void Alarm(int param)
{
WriteLine("滴滴滴。。水已经{0}度",param);
}
//显示
public void Display(int param)
{
WriteLine("水烧开了。。温度是{0}", param);
}
}
class Program
{
static void Main(string[] args)
{
Heater heater = new Heater();
heater.Operation += heater.Alarm; //注册方法
heater.Operation += heater.Display;
heater.BoilWater();
ReadKey();
}
}
}
.Net Framework的编码规范:
再做一下说明:
namespace Event_Delegate
{
class Heater
{
private int temperature;
public string producers = "红日集团";
//定义一个委托,这个委托的参数和我们要调用的方法的参数类型要保持一致
public delegate void BoiledEventHandler(Object sender, BoiledEventArgs e); //
//定义了一个事件
public event BoiledEventHandler Boiled;
//定义一个BoiledEventArgs继承自EventArgs
public class BoiledEventArgs : EventArgs
{
public readonly int temperature; //定义为只读变量
public BoiledEventArgs(int temperature) //结构函数
{
this.temperature = temperature; //
}
}
//烧水
public void BoilWater()
{
for (int i = 0; i <= 100; i++)
temperature = i;
if (temperature > 95)
{
BoiledEventArgs e = new BoiledEventArgs(temperature); //实例化BoiledEventArgs,将所感兴趣的信息传给方法
OnBiled(e);
}
}
//调用注册方法
public void OnBiled(BoiledEventArgs e)
{
//这里带入了两个参数,一个是我们对象本身,第二是我们的一个对象,这个对象包含了我们需要被观察的信息,这里就是Temperature
if (Boiled != null)
{
Boiled(this, e);
}
}
}
class Alarm
{
//警报
public void MakeAlarm(Object sender, Heater.BoiledEventArgs e)
{
Heater heater = (Heater)sender; //转换成Heater类
WriteLine("滴滴滴。。水已经{0}度", e.temperature);
WriteLine(heater.producers); //
}
}
class Display
{
//显示
public void MakeDisplay(Object sender, Heater.BoiledEventArgs e)
{
WriteLine("水烧开了。。温度是{0}", e.temperature);
}
}
class Program
{
static void Main(string[] args)
{
Alarm alarm = new Alarm();
Display display = new Display();
Heater heater = new Heater();
heater.Boiled += alarm.MakeAlarm;
heater.Boiled += display.MakeDisplay;
heater.BoilWater();
ReadKey();
}
}
}
欢迎光临 重工电子论坛 (http://cqutlab.cn/) | Powered by Discuz! X3.1 |