using System; namespace EventDemo { public delegate void CatBrayEventHandle(int score); public class sleeping { public event CatBrayEventHandle CatShout; int theScore; public int Score { get {return theScore;} set { if(theScore != value) { CatShout(value); } } } } public class MouseRun { public MouseRun(sleeping slp) { slp.CatShout += new CatBrayEventHandle(slp_CatShout); } private void slp_CatShout(int score) { if(score > 100) Console.WriteLine("老鼠: 有猫!快逃啊.."); else Console.WriteLine("老鼠: 猫虽然叫了, 声音不够大, 赫赫, 老鼠没听见!"); } } public class Master { public Master(sleeping sleep) { sleep.CatShout += new CatBrayEventHandle(sleep_CatShout); } private void sleep_CatShout(int score) { if(score > 100) Console.WriteLine("主人: 猫叫了!"); else Console.WriteLine("主人: 猫虽然叫了, 声音不够大, 主人没听见!"); } } /**//// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { /**//// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { sleeping slping = new sleeping(); MouseRun mr = new MouseRun(slping); Master ms = new Master(slping); //slping.Score = 66; slping.Score = 123; } }} 转载于:https://www.cnblogs.com/silva/archive/2006/04/09/370565.html