delegate void ActionDelegate();
class Cat
{
public event ActionDelegate OnCry;
public event ActionDelegate OnCry1;
public void Cry()
{
Console.WriteLine("请输入0或着1");
int i = Convert.ToInt32(Console.ReadLine());
if (i == 0)
{
Console.WriteLine("猫叫了");
OnCry();
}
else
{
Console.WriteLine("猫大叫一声");
OnCry1();
}
}
}
class Mouse
{
public Mouse(Cat cat)
{
cat.OnCry += new ActionDelegate(Run);
cat.OnCry1 += new ActionDelegate(Run1);
}
public void Run()
{
Console.WriteLine("老鼠跑了");
}
public void Run1()
{
Console.WriteLine("小老鼠吓得发疯的跑了,米老鼠也吓跑了");
}
}
class Master
{
public Master(Cat cat)
{
cat.OnCry += new ActionDelegate(Week);
cat.OnCry1 += new ActionDelegate(Week);
}
public void Week()
{
Console.WriteLine("主人醒了");
}
}
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat();
Mouse mouse = new Mouse(cat);
Master master = new Master(cat);
cat.Cry();
Console.ReadKey();
}
} |