现在的位置: 首页 > 综合 > 正文

C#定义静态事件

2012年08月10日 ⁄ 综合 ⁄ 共 579字 ⁄ 字号 评论关闭

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!");
        }
    }

抱歉!评论已关闭.