Example #1
0
        public static void Show()
        {
            BridegroomEven bridegroom = new BridegroomEven();
            // 实例化 Friend 对象
            Friend friend_zhangsan = new Friend("张三");
            Friend friend_lisi     = new Friend("李四");
            Friend friend_wangwu   = new Friend("王五");

            // 使用 += 来订阅事件
            bridegroom.MarryEvent += new MarrayHandler(friend_zhangsan.Reply);
            bridegroom.MarryEvent += new MarrayHandler(friend_lisi.Reply);

            // 发出通知, 只有订阅了事件的对象才能收到通知
            bridegroom.Notice("发出通知!");
            Console.WriteLine("-+-+-+-+-+");

            // 使用 -= 来取消事件订阅, 此时李四将收不到通知
            bridegroom.MarryEvent -= new MarrayHandler(friend_lisi.Reply);
            // 此时王五将收到通知
            bridegroom.MarryEvent += new MarrayHandler(friend_wangwu.Reply);

            // 发出通知
            bridegroom.Notice("发出通知!");
            Console.WriteLine("-+-+-+-+-+");
        }
Example #2
0
        public static void Show()
        {
            Util.PrintTitle(delegate() { DelegateUsage.Show(); }, "委托");
            Util.PrintTitle(delegate() { DelegateChain.Show(); }, "委托链");
            Util.PrintTitle(delegate() { BridegroomEven.Show(); }, "事件");

            /* 在类外调用事件是不允许的 */
            //bridegroomeven bridegroom_even = new bridegroomeven();
            //friend friend_zhaoliu = new friend("赵六");
            //bridegroom_even.marryevent += friend_zhaoliu.reply;
            //bridegroom_even.marryevent("msg");

            /* 可以在类外调用委托 */
            //DelegateChain.DelegateTest dt_static = new DelegateChain.DelegateTest(DelegateChain.Method1);
            //dt_static();

            Util.PrintTitle(delegate() { UseEventHandler.Show(); }, "EventHandler委托类");
            Util.PrintTitle(delegate() { UseEventArgs.Show(); }, "自定义EventArgs类用于事件的参数");
        }