码迷,mamicode.com
首页 > 其他好文 > 详细

事件委托

时间:2016-06-21 15:48:12      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

 //事件和委托实例
   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);
       }
   }

事件委托

标签:

原文地址:http://www.cnblogs.com/jeffry/p/5603574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!