C#委托事件的理解
public delegate void CatListeningHandler();
class Cat
{
public event CatListeningHandler CatCry;
public void Cry()
{
Console.WriteLine("Meow~~~");
OnCry();
}
protected virtual void OnCry()
{
if (CatCry != null)
{
CatCry();
}
}
}
class Mouse
{
public void Run()
{
Console.WriteLine("Mouse run away……");
}
}
class Program
{
static void Main(string[] args)
{
Cat obj_cat = new Cat();
Mouse obj_mouse = new Mouse();
obj_cat.CatCry += new CatListeningHandler(obj_mouse.Run);
obj_cat.Cry();
Console.ReadLine();
}
}
每天进步一点点...