//3.编写引发事件的函数,注意多了个参数; public void OnAlarm(NumberOfThiefEventArgs e) { if (this.Alarm != null) { Console.WriteLine("/n狗报警: 有小偷进来了,汪汪~~~~~~~/n"); this.Alarm(this, e); } }
//4.编写事件处理程序,参数中包含着numberOfThief信息 void HostHandleAlarm(object sender, NumberOfThiefEventArgs e) { if (e.numberOfThief <= 1) { Console.WriteLine("主 人: 抓住了小偷!"); } else { Console.WriteLine("主 人:打110报警,我家来了{0}个小偷!", e.numberOfThief); } }
static void Main(string[] args) { NumberOfThiefEventArgs NumberOfThiefEventArgs = new NumberOfThiefEventArgs(1); 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("当前时间: " + now); System.Threading.Thread.Sleep(1000); //程序暂停一秒 now = now.AddSeconds(1); //时间增加一秒 } //午夜零点小偷到达,看门狗引发Alarm事件 Console.WriteLine("/n月黑风高的午夜: " + now); Console.WriteLine("小偷悄悄地摸进了主人的屋内... "); //创建事件参数 NumberOfThiefEventArgs e = new NumberOfThiefEventArgs(3); dog.OnAlarm(e); var class1 = typeof(Class1); // var assembly = Assembly.Load(); // assembly.CreateInstance // class1.a Console.Read(); }