C#定义静态事件

来源:互联网 发布:sql union group 编辑:程序博客网 时间:2024/05/25 05:36
class Employee
    {
        public delegate void DelEvent(int ID);
        public static event DelEvent PlayEvent;
        public int ID { get; set; }


        public Employee(int id)
        {
            ID = id;
            Play(id);
        }


        public void Play(int ID)
        {
            PlayEvent(ID);
        }
    }


    class MainProgram
    {
        static void Main()
        {
            Employee.PlayEvent += new Employee.DelEvent(Employee_PlayEvent);
            for (int i = 0; i < 10; ++i)
            {
                Employee employee = new Employee(i);
            }
            Console.ReadKey();
        }


        static void Employee_PlayEvent(int ID)
        {
            Console.WriteLine("Employee " + ID.ToString() + " is Playing game!");
        }
    }
原创粉丝点击