zoukankan      html  css  js  c++  java
  • 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();
    //        }
    //    }
    //}

  • 相关阅读:
    172. Factorial Trailing Zeroes
    96. Unique Binary Search Trees
    95. Unique Binary Search Trees II
    91. Decode Ways
    LeetCode 328 奇偶链表
    LeetCode 72 编辑距离
    LeetCode 226 翻转二叉树
    LeetCode 79单词搜索
    LeetCode 198 打家劫舍
    LeetCode 504 七进制数
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1516926.html
Copyright © 2011-2022 走看看