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();
    //        }
    //    }
    //}

  • 相关阅读:
    startx
    BIOS above 4g setting
    How to download file from google drive
    凤凰财经
    SOTA state-of-the-art model
    update gpu drivers
    /opt/nvidia/deepstream/deepstream/sources/libs/nvdsinfer_customparser/nvdsinfer_custombboxparser.cpp
    retinanet keras
    fp16 fp32 int8
    tf.gather with tf.Session() as sess:
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1516926.html
Copyright © 2011-2022 走看看