一个经典的例子:猫叫了,老鼠逃跑,主人醒来。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    //一个经典的例子:猫叫了,老鼠逃跑,主人醒来。
    class Program
    {
        
        /// <summary>
        /// 猫叫事件处理
        /// </summary>
        /// <param name="score">猫叫的声音大小</param>
        public delegate void CatBrayEventHandle(int score);
        static void Main(string[] args)
        {

            Sleeping slp = new Sleeping();
            MouseRun MR = new MouseRun(slp);
            Master M = new Master(slp);
            slp.Score = 90;

            Console.ReadKey();
        }

        public class Sleeping
        {
            public event CatBrayEventHandle CatShout;

            int fScore;

            public int Score
            {
                get { return fScore; }
                set { if (value != fScore) CatShout(value); }
            }
        }



        public class MouseRun
        {
            public MouseRun(Sleeping sleeping)
            {
                sleeping.CatShout += new CatBrayEventHandle(sleeping_CatShout);
            }

            void sleeping_CatShout(int score)
            {
                if (score > 100)
                {
                    Console.WriteLine("老鼠:有猫,快跑。");
                }
                else
                {
                    Console.WriteLine("猫叫声音太小了,老鼠没有听见。");
                }
            }
        }

        public class Master
        { 
            public Master(Sleeping sleeping)
            {
                sleeping.CatShout += new CatBrayEventHandle(sleeping_CatShout);
            }

            void sleeping_CatShout(int score)
            {
                if (score > 100)
                {
                    Console.WriteLine("主人:猫叫了。");
                }
                else
                {
                    Console.WriteLine("猫叫了,声音太小,主人没有听见。");
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值