参考代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleAppFirst.Demo
{
class Alarm_Publisher
{
public delegate void Dele_Alarm(Args_Class e);//委托的定义
public event Dele_Alarm Alarm;//事件的声明
//发布事件
public void AlarmAt(Args_Class e)
{
if (Alarm != null)//当事件发生时
Alarm(e);//调用事件处理程序
}
}
class Args_Class//当事件发生时携带的数据
{
public int second;
public int minute;
public int hour;
}
class Order
{
//处理程序
public static void GetUp(Args_Class e)
{
Console.WriteLine(e.hour+e.minute+"点了,起床了");
}
}
class EventDemo
{
static void Main()
{
Alarm_Publisher al = new Alarm_Publisher();
al.Alarm += new Alarm_Publisher.Dele_Alarm(Order.GetUp);//代表事件发生了
Args_Class ar = new Args_Class();
ar.hour = 7;
al.AlarmAt(ar);//处理事件
}
//static void al_Alarm()
//{
// throw new Exception("The method or operation is not implemented.");
//}
}
}
委托和是和事件是的内存分配是怎样的????
|