class cat { public event EventHandler miao;
protected virtual void onmiao(EventArgs e) { if (miao != null) miao(this, e);
}
public void domiao() { Console.WriteLine("cat miao"); onmiao(EventArgs.Empty); } }
class mouse { public void run(object sender, EventArgs e) { Console.WriteLine("mouse run.."); } } class owner { public void noise(object sender, EventArgs e) { if (sender is cat) Console.WriteLine("heard cat miao.."); else Console.WriteLine("heard mouse noise"); Console.WriteLine("owner wake..."); } }
class Program { static void Main(string[] args) {
cat cat = new cat(); mouse m = new mouse(); owner o = new owner(); cat.miao += m.run; cat.miao += o.noise; cat.domiao(); } }
转载于:https://www.cnblogs.com/wolfcool/archive/2009/06/10/1500971.html
相关资源:数据结构—成绩单生成器