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("猫叫了,声音太小,主人没有听见。");
}
}
}
}
}
一个经典的例子:猫叫了,老鼠逃跑,主人醒来。
最新推荐文章于 2020-04-18 10:47:23 发布