//3.編寫引發事件的函數;
public void OnAlarm()
{
if (this.Alarm != null)
{
Console.WriteLine("\n狗報警: 有小偷進來了,汪汪~~~~~~~");
this.Alarm(this, new EventArgs()); //發出警報
}
}
}
//事件接收者
class Host
{
//4.編寫事件處理程序
void HostHandleAlarm(object sender, EventArgs e)
{
Console.WriteLine("主 人: 抓住了小偷! ");
}
//5.注冊事件處理程序
public Host(Dog dog)
{
dog.Alarm += new Dog.AlarmEventHandler(HostHandleAlarm);
}
}
//6.現在來觸發事件
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
Host host = new Host(dog);
//當前時間,從2008 年12月31日23:59:50開始計時
DateTime now = new DateTime (2008,12,31,23,59,50);
DateTime midnight = new DateTime(2009, 1, 1, 0, 0, 0);
//等待午夜的到來
Console.WriteLine("時間一 秒一秒地流逝...");
while (now < midnight)
{
Console.WriteLine("當前