.net 委托和事件

来源:互联网 发布:python sleep import 编辑:程序博客网 时间:2024/05/22 05:15
委托和事件在.net中是至关重要的,学好委托和事件可以让我们写出更简洁的代码,为我们为后面的学习作为铺垫委托:生活中的委托比如我们想要完成一件事情,而我们自己不想完成 想让别人代替我们完成 这就是委托。在程序中的委托:  我们知道c#是面向对象的   把生活中的情境放到程序里面就是方法(生活中人的行为在程序中就是方法) 比如说一个下载程序要显示下载进度条,而进度条不想放在下载的方法里面显示(自己不愿意干)  就可以利用委托把显示进度条的任务交个显示页面的方法去完成(别人帮你完成)这就是程序中的委托。事件:生活中的事件想必大家也很好理解比如 猫发出叫声(事件发起者)  老鼠(事件接收者)听见会逃跑这就是事件。程序中的事件:在程序中事件的本质也是委托  时间是由委托发起的  也就是说事件本身不会发生  通过委托来进行事件发起。事件创建语法:访问修饰符+event(事件关键字)+委托类型+ 事件名委托创建语法:访问修饰符+delegate+返回值类型+委托名(参数列表);注意:委托是一中类型  是自定义类型 他的类型就是方法名称好了  基础知识说过了 我们下面直接看代码   ```using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication16{    /// <summary>    /// 测试.net事件    /// </summary>    class Program    {        static void Main(string[] args)        {            Cat cat = new Cat("Tom", "白色");            Mouse mou = new Mouse("Jerry", "黑色");            //+=添加一个事件  -=取消一个事件            cat.CatShoutEvent += new Cat.CatShoutEventHandler(mou.Run);            cat.Shor();            Console.ReadKey();        }    }    /// <summary>    /// 猫类    /// </summary>    class Cat {        public string name;        public string color;        public Cat(string name,string color) {            this.name = name;            this.color = color;        }        /// <summary>        /// 猫叫的方法(发出事件)        /// </summary>        public void Shor() {            Console.WriteLine("一只名字为"+this.name+"颜色为"+this.color+"的小猫发出了叫声——喵喵喵");            if (CatShoutEvent!=null)            {                CatShoutEvent(this, new CatShoutEventArgs() { catColor=this.name, catName=this.color });            }        }            //定义一个委托(处理猫叫的事件处理)        public delegate void CatShoutEventHandler(object sender, CatShoutEventArgs e);        //定义一个事件(创建事件的语法:访问修饰符(public  event  委托方法名(或委托返回类型) 事件名))        public event CatShoutEventHandler CatShoutEvent;          }    //事件需要传送的参数必须继承EventArgs类    class CatShoutEventArgs : EventArgs {        public string catColor { get; set; }        public string catName { get; set; }    }    /// <summary>    /// 鼠类    /// </summary>    class Mouse {        public string name;        public string color;        public Mouse(string name, string color)        {            this.name = name;            this.color = color;        }        /// <summary>        /// 老鼠逃跑的方法(事件接受者)        /// </summary>        public void Run(object sender,CatShoutEventArgs  e) {           Cat  obj= sender as Cat;//事件发起者            Console.WriteLine("事件发起者的名字:"+obj.name);            Console.WriteLine("传过来的参数是:"+e.catName);            Console.WriteLine(name + " 的老鼠 " + color + " 说:\"老猫来了,快跑!\"  \n我跑!!\n我使劲跑!!\n我加速使劲跑!!!\n");        }    }}运行结果图:![这里写图片描述](http://img.blog.csdn.net/20171222115858087?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVGVzdERhdGFz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)```代码中需要解释的就是事件参数  在代码中Mouse这个类中的Run方法接收两个参数第一个是object类型的 sender这个参数是指事件的发起者的信息,第二个参数是CatShoutEventArgs类型的  他是继承雨EventArgs类的 (如果事件不需要参数这个可以忽略不写)  这个类是事件参数的信息 比如想获取事件发起者的信息 就可以用以下代码  Cat  obj= sender as Cat;    Console.WriteLine("事件发起者的名字:"+obj.name);