C#_事件的使用

来源:互联网 发布:windows无法激活怎么办 编辑:程序博客网 时间:2024/04/28 09:39
    class Person    {        // 委托如果是public修饰,就可以在类的外部调用        public delegate void behaviourDelegate();// 委托        // 事件只能在定义该事件的类的内部调用,不管是public修饰,还是private修饰,都只能在类的内部调用        public event behaviourDelegate behaviourEvent;// 事件        public void notify()        {            if (this.behaviourEvent != null)            {                this.behaviourEvent();            }        }        public void sayEvent()        {            Console.WriteLine("sayEvent");        }        public void talkEvent()        {            Console.WriteLine("talkEvent");        }        public void walkEvent()        {            Console.WriteLine("walkEvent");        }    }    class Start    {        static void Main(string[] args)        {            var per = new Person();            per.behaviourEvent += per.sayEvent;            per.behaviourEvent += per.talkEvent;            per.behaviourEvent += per.walkEvent;            per.notify();            Console.ReadLine();        }    }

0 0
原创粉丝点击