• Event and Delegate


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


    namespace EventDelegate
    {
        class CatShoutEventArgs:EventArgs
        {
            private string name;
            public string Name
            {
                get{return name;}
                set{name = value;}
            }
        }
        class Cat
        {
            private string name;
            public Cat(string name)
            {
                this.name = name;
            }
            public delegate void CatShoutEventHandler(object sender,CatShoutEventArgs args);
            public CatShoutEventHandler CatShout;
     
            public void Shout()
            {
                Console.WriteLine("miao! i am {0}!",name);
                if (CatShout != null)
                {
                    CatShoutEventArgs e = new CatShoutEventArgs();
                    e.Name = this.name;
                    CatShout(this,e);
                }
            }

        }
        class Mouse
        {
            private string name;
            public Mouse(string name)
            {
                this.name = name;
            }
            public void Run(object sender,CatShoutEventArgs args)
            {
                Console.WriteLine("{0} is coming, {1} run!",args.Name,name);
                   
            }
        }
        class Program
        {
            static void Main()
            {
                Cat cat = new Cat("Jerry");
                Mouse m1 = new Mouse("fifi");
                Mouse m2 = new Mouse("lili");

                cat.CatShout += new Cat.CatShoutEventHandler(m1.Run);
                cat.CatShout += new Cat.CatShoutEventHandler(m2.Run);
     
                cat.Shout();

                Console.Read();
            }
        }
    }
    //namespace EventDelegate
    //{
    //    class Cat
    //    {
    //        private string name;
    //        public Cat(string name)
    //        {
    //            this.name = name;
    //        }
    //        public delegate void CatShoutEventHandler();

    //        public CatShoutEventHandler CatShout;
    //        public void Shout()
    //        {
    //            Console.WriteLine("Miao! i am {0}.", name);
    //            if (CatShout != null)
    //            {
    //                CatShout();
    //            }
    //        }
    //    }
    //    class Mouse
    //    {
    //        private string name;
    //        public Mouse(string name)
    //        {
    //            this.name = name;
    //        }
    //        public void Run()
    //        {
    //            Console.WriteLine("Cat is coming! {0} run !", name);
    //        }
    //    }
    //    class Program
    //    {
    //        static void Main(string[] args)
    //        {
    //            Cat cat = new Cat("Jerry");
    //            Mouse m1 = new Mouse("Mini");
    //            Mouse m2 = new Mouse("Mimi");

    //            cat.CatShout += new Cat.CatShoutEventHandler(m1.Run);
    //            cat.CatShout += new Cat.CatShoutEventHandler(m2.Run);

    //            cat.Shout();

    //            Console.Read();
    //        }
    //    }
    //}

  • 相关阅读:
    基于yum的方式安装Cloudera Manager Agent(使用Mysql 8.0版本)
    数据库基础SQL知识面试题二
    Zabbix Server 自带模板监控更加灵活MySQL数据库
    Zabbix Server 自带模板监控有密码MySQL数据库
    Zabbix Server 自带模板监控无密码MySQL数据库
    Zabbix Server 监控Web页面
    HDFS集群常见报错汇总
    Hadoop ha CDH5.15.1-hadoop集群启动后,集群容量不正确,莫慌,这是正常的表现!
    FastDFS + Nginx代理方式访问
    FastDFS 分布式文件系统部署实战及基本使用
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1516926.html
Copyright © 2020-2023  润新知