猫叫了,老鼠跑了!(复习委托和事件)

  定义CatShoutEventArgs类(继承EventArgs),用来在事件触发时传递数据(猫的名字):

    public class CatShoutEventArgs : EventArgs
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

定义Cat类:

    public class Cat
    {
        private string name;
        
        public Cat() { }

        public Cat(string name)
        {
            this.name = name;
        }

        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();
                //猫的名字
                e.Name = this.name;

                //当事件触发时,通知所有登记过的对象,并将发送通知的自己(sender:cat)
                //以及需要的数据(CatShoutEventArgs:Name)传递过去
                CatShout(this, e); 
            }
        }
    }

定义Mouse类:

 public class Mouse
    {
        private string name;
        
        public Mouse() { }

        public Mouse(string name)
        {
            this.name = name;
        }

        /// <summary>
        /// 逃跑的方法有两个参数,并且可以在显示时,说出老猫的名字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void Run(object sender, CatShoutEventArgs args)
        {
            Console.WriteLine("老猫{0}来了,{1}快跑!",args.Name,this.name);
        }
    }

最后Main方法里实例化一只猫和两只老鼠,注册并触发猫叫事件:

static void Main(string[] args)
        {
            Cat cat = new Cat("Tom");
            Mouse mouse1 = new Mouse("Jerry");
            Mouse mouse2 = new Mouse("Jack");

            //将Mouse的Run方法(Run(object sender, CatShoutEventArgs args))通过实例化委托Cat.CatShoutEventHandler
            //登记到Cat的事件CatShout当中
            cat.CatShout += new Cat.CatShoutEventHandler(mouse1.Run);
            cat.CatShout += new Cat.CatShoutEventHandler(mouse2.Run);

            cat.Shout();

            Console.ReadKey();
        }

运行结果如下图:

程序完整代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EventArgsDemo
{

    public class CatShoutEventArgs : EventArgs
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

    public class Cat
    {
        private string name;
        
        public Cat() { }

        public Cat(string name)
        {
            this.name = name;
        }

        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();
                //猫的名字
                e.Name = this.name;

                //当事件触发时,通知所有登记过的对象,并将发送通知的自己(sender:cat)
                //以及需要的数据(CatShoutEventArgs:Name)传递过去
                CatShout(this, e); 
            }
        }
    }

    public class Mouse
    {
        private string name;
        
        public Mouse() { }

        public Mouse(string name)
        {
            this.name = name;
        }

        /// <summary>
        /// 逃跑的方法有两个参数,并且可以在显示时,说出老猫的名字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void Run(object sender, CatShoutEventArgs args)
        {
            Console.WriteLine("老猫{0}来了,{1}快跑!",args.Name,this.name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Cat cat = new Cat("Tom");
            Mouse mouse1 = new Mouse("Jerry");
            Mouse mouse2 = new Mouse("Jack");

            //将Mouse的Run方法(Run(object sender, CatShoutEventArgs args))通过实例化委托Cat.CatShoutEventHandler
            //登记到Cat的事件CatShout当中
            cat.CatShout += new Cat.CatShoutEventHandler(mouse1.Run);
            cat.CatShout += new Cat.CatShoutEventHandler(mouse2.Run);

            cat.Shout();

            Console.ReadKey();
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/527289276qq/p/5349326.html