using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EventDelegate
{
class CatShoutEventArgs:EventArgs
{
private string name;
public string Name
{
get{return name;}
set{name = value;}
}
}
class Cat
{
private string name;
public Cat(string name)
{
this.name = name;
}
public delegate void CatShoutEventHandler(object sender,CatShoutEventArgs args);
public CatShoutEventHandler CatShout;
public void Shout()
{
Console.WriteLine("miao! i am {0}!",name);
if (CatShout != null)
{
CatShoutEventArgs e = new CatShoutEventArgs();
e.Name = this.name;
CatShout(this,e);
}
}
}
class Mouse
{
private string name;
public Mouse(string name)
{
this.name = name;
}
public void Run(object sender,CatShoutEventArgs args)
{
Console.WriteLine("{0} is coming, {1} run!",args.Name,name);
}
}
class Program
{
static void Main()
{
Cat cat = new Cat("Jerry");
Mouse m1 = new Mouse("fifi");
Mouse m2 = new Mouse("lili");
cat.CatShout += new Cat.CatShoutEventHandler(m1.Run);
cat.CatShout += new Cat.CatShoutEventHandler(m2.Run);
cat.Shout();
Console.Read();
}
}
}
//namespace EventDelegate
//{
// class Cat
// {
// private string name;
// public Cat(string name)
// {
// this.name = name;
// }
// public delegate void CatShoutEventHandler();
// public CatShoutEventHandler CatShout;
// public void Shout()
// {
// Console.WriteLine("Miao! i am {0}.", name);
// if (CatShout != null)
// {
// CatShout();
// }
// }
// }
// class Mouse
// {
// private string name;
// public Mouse(string name)
// {
// this.name = name;
// }
// public void Run()
// {
// Console.WriteLine("Cat is coming! {0} run !", name);
// }
// }
// class Program
// {
// static void Main(string[] args)
// {
// Cat cat = new Cat("Jerry");
// Mouse m1 = new Mouse("Mini");
// Mouse m2 = new Mouse("Mimi");
// cat.CatShout += new Cat.CatShoutEventHandler(m1.Run);
// cat.CatShout += new Cat.CatShoutEventHandler(m2.Run);
// cat.Shout();
// Console.Read();
// }
// }
//}