using System;
class Program
{
public class Cat
{
private string name;
public Cat(string name)
{
this.name = name;
}
public delegate void CatCallEventHandler(); //猫叫的委托
public event CatCallEventHandler catevent; //猫叫事件
public void Call()
{
Console.WriteLine("猫{0}叫:喵,喵,喵", name);
if(catevent != null)
catevent();
}
}
public class Mouse
{
private string name;
public Mouse(Cat c, string name)
{
this.name = name;
c.catevent += new Cat.CatCallEventHandler(this.Run); //注册事件
}
public void Run()
{
Console.WriteLine("老鼠{0}开始逃跑", name);
}
}
static void Main(string[] args)
{
Cat c = new Cat("King");
Mouse m1 = new Mouse(c, "Tom1");
Mouse m2 = new Mouse(c, "Tom2");
c.Call();
Console.ReadKey();
}
}