主人 猫 老鼠
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();
}
}