static void Main(string[] args) { Host host1 = new Host(); host1.Name = "我零0七"; Thief thief1 = new Thief(); thief1.Name = "李三"; Dog dog1 = new Dog(); dog1.Name = "小白"; Neighbor neighbor1 = new Neighbor(); neighbor1.Name = MethodBase.GetCurrentMethod().ReflectedType.Namespace;//ReflectedType获得程序(当前)对象 //Action BarEvent = thief1.Run;//Action声明委托 //BarEvent += host1.Wake;//添加委托 //BarEvent += neighbor1.Wake;//添加委托 Action BarEvent = () => { Console.WriteLine(thief1.Name + "害怕的跑了跑了"); }; BarEvent += () => { Console.WriteLine(host1.Name + "醒了,起来看看怎么回事"); }; BarEvent += () => { Console.WriteLine("不明真相的" + neighbor1.Name); }; Console.WriteLine("夜深了!"); host1.Sleeep(); if (Console.ReadLine() == "贼来了") { dog1.Bark(); BarEvent();//执行事件 } Console.ReadKey(); }
static void Main(string[] args) { Host host1 = new Host(); host1.Name = "我零0七"; Thief thief1 = new Thief(); thief1.Name = "李三"; Dog dog1 = new Dog(); dog1.Name = "小白"; Neighbor neighbor1 = new Neighbor(); neighbor1.Name = MethodBase.GetCurrentMethod().Attributes.GetType().Namespace; dog1.BarkEvent += thief1.Run; //事件处理方法 dog1.BarkEvent += host1.Wake; //事件处理方法 dog1.BarkEvent += neighbor1.Wake; Console.WriteLine("夜深了!"); host1.Sleeep(); if (Console.ReadLine() == "贼来了") { dog1.Bark(); } Console.ReadKey(); }