事件委托

 //事件和委托实例
   public class Xiaoshou
    {
        public string Mingzi { get; set; }
        public delegate void WaiqinEventHandler(object e, WaiqinEventArgs arg);
        public event WaiqinEventHandler WaiqinHanlder;
        public Xiaoshou(string mingzi)
        {
            Mingzi = mingzi;
        }
        public void WaiqinLa(object e, WaiqinEventArgs arg)
        {
            if (WaiqinHanlder != null)
            {
                WaiqinHanlder(e, arg);
            }
        }
    }
   public class WaiqinEventArgs : EventArgs {
       public string Msg { get;set;}
       public DateTime EventTime { get; set; }
       public WaiqinEventArgs(string msg) {
           Msg = msg;
           EventTime = DateTime.Now;
       }
   }
   public class KaoqinDandang
   {
       public KaoqinDandang(string name) {
           Console.WriteLine("我是考勤担当;"+name);
       }
       public void Kaoqin(object e, WaiqinEventArgs args)
       {
           Console.WriteLine("考勤担当确认考勤,确认用户:;" + (e as Xiaoshou).Mingzi+"的考勤;");
       }    
    }
   public class boss
   {
       public boss(string name)
       {
           Console.WriteLine("我是老板:" + name);
       }
       public void shenhe(object e, WaiqinEventArgs args)
       {
           Console.WriteLine("老板认可了用户:;" + (e as Xiaoshou).Mingzi + "的外勤;");
       }
   }
   public class program {
       static void main() {
           Xiaoshou xs = new Xiaoshou("小李子");
           KaoqinDandang kd = new KaoqinDandang("小六子");
           boss bs = new boss("大牛子");
           WaiqinEventArgs args=new WaiqinEventArgs("我要外勤");
           
           xs.WaiqinHanlder += new Xiaoshou.WaiqinEventHandler(kd.Kaoqin);
           xs.WaiqinHanlder += new Xiaoshou.WaiqinEventHandler(bs.shenhe);
           xs.WaiqinLa(xs, args);
       }
   }

posted @ 2016-06-21 14:37  jeffery1010  Views(188)  Comments(0Edit  收藏  举报