委托和事件实例

    /// <summary>
    /// 带形参的举杯委托RaiseEventHandler
    /// </summary>
    /// <param name="hand"></param>
    delegate void RaiseEventHandler(string hand);
    /// <summary>
    /// 不带形参的摔杯委托FallEventHandler
    /// </summary>
    delegate void FallEventHandler();

    /// <summary>
    /// 首领A
    /// </summary>
    class A
    {
        /// <summary>
        /// 首领A举杯事件
        /// </summary>
        public event RaiseEventHandler RaiseEvent;
        /// <summary>
        /// 首领A摔杯事件
        /// </summary>
        public event FallEventHandler FallEvent;

        /// <summary>
        /// 举杯
        /// </summary>
        /// <param name="hand">手:左、右</param>
        public void Raise(string hand)
        {
            Console.WriteLine("首领A-{0}手举杯", hand);
            // 调用举杯事件,传入左或右手作为参数
            if (RaiseEvent != null)
            {
                RaiseEvent(hand);
            }
        }

        /// <summary>
        /// 摔杯
        /// </summary>
        public void Fall()
        {
            Console.WriteLine("首领A摔杯");
            // 调用摔杯事件
            if (FallEvent != null)
            {
                FallEvent();
            }
        }
    }
    /// <summary>
    /// 部下B
    /// </summary>
    class B
    {
        A a;
        public B(A a)
        {
            this.a = a;
            // 订阅举杯事件
            a.RaiseEvent += new RaiseEventHandler(a_RaiseEvent);
            // 订阅摔杯事件
            a.FallEvent += new FallEventHandler(a_FallEvent); 
        }

        /// <summary>
        /// 首领举杯时的动作
        /// </summary>
        /// <param name="hand">若首领A左手举杯,则B攻击</param>
        void a_RaiseEvent(string hand)
        {
            if (hand.Equals("左"))
            {
                Attack();
            }
        }

        /// <summary>
        /// 首领摔杯时的动作
        /// </summary>
        void a_FallEvent()
        {
            Attack();
        }

        /// <summary>
        /// 攻击
        /// </summary>
        public void Attack()
        {
            Console.WriteLine("部下B发起攻击");
        }
    }
    /// <summary>
    /// 部下C
    /// </summary>
    class C
    {
        A a;
        public C(A a)
        {
            this.a = a;
            // 订阅举杯事件
            a.RaiseEvent += new RaiseEventHandler(a_RaiseEvent);
            // 订阅摔杯事件
            a.FallEvent += new FallEventHandler(a_FallEvent); 
        }

        /// <summary>
        /// 首领举杯时的动作
        /// </summary>
        /// <param name="hand">若首领A左手举杯,则B攻击</param>
        void a_RaiseEvent(string hand)
        {
            if (hand.Equals("右"))
            {
                Attack();
            }
        }

        /// <summary>
        /// 首领摔杯时的动作
        /// </summary>
        void a_FallEvent()
        {
            Attack();
        }

        /// <summary>
        /// 攻击
        /// </summary>
        public void Attack()
        {
            Console.WriteLine("部下C发起攻击");
        }
    }

转载,做了部分优化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值