using System;
using System.Collections.Generic;
using System.Text;
namespace delegateEvent
{
class callEventArgs : System.EventArgs
{
private string Name;
public callEventArgs(string CName)
{
this.Name = CName;
}
public string name
{
get
{
return Name;
}
set
{
this.Name = value;
}
}
}
public delegate void CallEventHandler(object sender,callEventArgs e);
class Cat
{
public event CallEventHandler Call;
public virtual void OnCall(callEventArgs e)
{
if (Call != null)
{
Call(this, e);
}
}
public void Calling(string CatName)
{
Console.WriteLine("猫大叫");
OnCall(new callEventArgs(CatName));
}
}
class Mouse
{
private string Name;
public Mouse(string MName)
{
this.Name = MName;
}
public string mName
{
get
{
return Name;
}
set
{
this.Name = value;
}
}
public void Response(object sender, callEventArgs e)
{
Console.WriteLine(Name+"四处逃跑");
}
}
class Person
{
private string Name;
public Person(string PName)
{
this.Name = PName;
}
public string pName
{
get
{
return Name;
}
set
{
this.Name = value;
}
}
public void Response(object sender, callEventArgs e)
{
Console.WriteLine(Name + "醒了");
}
}
class Relation
{
static void Main()
{
Cat cat = new Cat();
Mouse mouse = new Mouse("kitty");
Person men = new Person("Person");
cat.Call+=new CallEventHandler(mouse.Response);
cat.Call+=new CallEventHandler(men.Response);
cat.Calling("cat");
}
}
}