天天看点

C#委托和事件 示例

//源码参考《大话设计模式》--清华大学出版社  程杰  著

using System;

using System.Collections.Generic;

using System.Text;

namespace 委托和事件

{

    class Program

    {

        static void Main(string[] args)

        {

            //实例化猫Tom和老鼠Jerry和Jack

            Cat cat = new Cat("Tom");

            Mouse mouse1 = new Mouse("Jerry");

            Mouse mouse2 = new Mouse("Jack");

            //将Mouse的Run方法,通过实例化委托Cat.CatShoutEventHandler登记到Cat的CatShout事件当中。其中“+=”表示增加委托实例对象,即"add_CatShout"

            cat.CatShout += new Cat.CatShoutEventHandler(mouse1.Run);

            cat.CatShout += new Cat.CatShoutEventHandler(mouse2.Run);

            cat.Shout();

            Console.WriteLine("\r");

            Cat1 cat1 = new Cat1("(有参)猫");

            Mouse1 mouse3 = new Mouse1("(有参)Jerry");

            Mouse1 mouse4 = new Mouse1("(有叁)Jack");

            //写法不变

            cat1.CatShout += new Cat1.CatShoutEventHandler(mouse3.Run);

            cat1.CatShout += new Cat1.CatShoutEventHandler(mouse4.Run);

            cat1.Shout();

            Console.Read();

        }

    }

    //无参数委托事件

    class Cat

    {

        private string name;

        public Cat(string name)

        {

            this.name = name;

        }

        //申明无参数委托  CatShoutEventHandler

        public delegate void CatShoutEventHandler();

        //申明事件CatShout,它的类型是委托CatShoutEventHandler

        public event CatShoutEventHandler CatShout;

        public void Shout()

        {

            Console.WriteLine("喵,我是{0}.", name);

            if (CatShout != null)

            {

                //由于CatShout的类型是委托CatShoutEventHandler()(无参数),所以CatShout()也是无参数,无返回值的,有参数的情况见下面的示例

                CatShout();//当执行Shou()方法时,若CatShou中有对象登记事件,则执行CatShout()方法

            }

        }

    }

    class Mouse

    {

        private string name;

        public Mouse(string name)

        {

            this.name = name;

        }

        public void Run()

        {

            Console.WriteLine("老猫来了,{0}快跑!", name);

        }

    }

    //有参数委托事件

    //增加一个新类,继承EventArgs。作用:在事件触发时,传递数据。

    public class CatShoutEventArgs : EventArgs  //EventArgs:包含事件数据的类的基类

    {

        private string name;

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

    }

    class Cat1

    {

        private string name;

        public Cat1(string name)

        {

            this.name = name;

        }

        //申明有参数的委托  object对象 sender是指向发送通知的对象;CatShoutEventArgs的 args包含了所有知道接收都所需要附件的信息

        public delegate void CatShoutEventHandler(object sender, CatShoutEventArgs args);

        public event CatShoutEventHandler CatShout;

        public void Shout()

        {

            Console.WriteLine("喵,我是{0}.", name);

            if (CatShout != null)

            {

                CatShoutEventArgs e = new CatShoutEventArgs();//申明并实例化一个CatShoutEventArgs

                e.Name = this.name;               //给name属性赋值为猫的名字

                CatShout(this, e);//当事件触发时,通过所有登记过的对象,并将发送通知的自己和所需要的信息传递过去

            }

        }

    }

    class Mouse1

    {

        private string name;

        public Mouse1(string name)

        {

            this.name = name;

        }

        //增加两个参数,以便接收猫的名字

        public void Run(object sender, CatShoutEventArgs args)

        {

            Console.WriteLine("老猫{0}来了,{1}快跑!", args.Name, name);

        }

    }

}