zoukankan      html  css  js  c++  java
  • 猫叫了,老鼠跑了!(复习委托和事件)

      定义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
  • 相关阅读:
    Selenium+Pytest自动化测试框架实战
    WPF性能优化经验总结
    C#跨窗体调用控件
    C# lock
    硬实时系统,到底多硬才算Hard Real Time System
    [GPIO]推荐一种超简单的硬件位带bitband操作方法,让变量,寄存器控制,IO访问更便捷,无需用户计算位置
    【STM32F407的DSP教程】第50章 STM32F407的样条插补实现,波形拟合丝滑顺畅
    实战技能分享,如何让工程代码各种优化等级通吃,含MDK AC5,AC6,IAR和GCC
    【深入探讨】DMA到底能不能起到加速程序执行的作用,DMA死等操作是否合理,多个DMA数据流同时刷是否处理过来
    《安富莱嵌入式周报》第238期:2021.11.012021.11.07
  • 原文地址:https://www.cnblogs.com/527289276qq/p/5349326.html
Copyright © 2011-2022 走看看