/// <summary>
/// 带形参的举杯委托RaiseEventHandler
/// </summary>
/// <param name="hand"></param>
delegate void RaiseEventHandler(string hand);
/// <summary>
/// 不带形参的摔杯委托FallEventHandler
/// </summary>
delegate void FallEventHandler();
/// <summary>
/// 首领A
/// </summary>
class A
{
/// <summary>
/// 首领A举杯事件
/// </summary>
public event RaiseEventHandler RaiseEvent;
/// <summary>
/// 首领A摔杯事件
/// </summary>
public event FallEventHandler FallEvent;
/// <summary>
/// 举杯
/// </summary>
/// <param name="hand">手:左、右</param>
public void Raise(string hand)
{
Console.WriteLine("首领A-{0}手举杯", hand);
// 调用举杯事件,传入左或右手作为参数
if (RaiseEvent != null)
{
RaiseEvent(hand);
}
}
/// <summary>
/// 摔杯
/// </summary>
public void Fall()
{
Console.WriteLine("首领A摔杯");
// 调用摔杯事件
if (FallEvent != null)
{
FallEvent();
}
}
}
/// <summary>
/// 部下B
/// </summary>
class B
{
A a;
public B(A a)
{
this.a = a;
// 订阅举杯事件
a.RaiseEvent += new RaiseEventHandler(a_RaiseEvent);
// 订阅摔杯事件
a.FallEvent += new FallEventHandler(a_FallEvent);
}
/// <summary>
/// 首领举杯时的动作
/// </summary>
/// <param name="hand">若首领A左手举杯,则B攻击</param>
void a_RaiseEvent(string hand)
{
if (hand.Equals("左"))
{
Attack();
}
}
/// <summary>
/// 首领摔杯时的动作
/// </summary>
void a_FallEvent()
{
Attack();
}
/// <summary>
/// 攻击
/// </summary>
public void Attack()
{
Console.WriteLine("部下B发起攻击");
}
}
/// <summary>
/// 部下C
/// </summary>
class C
{
A a;
public C(A a)
{
this.a = a;
// 订阅举杯事件
a.RaiseEvent += new RaiseEventHandler(a_RaiseEvent);
// 订阅摔杯事件
a.FallEvent += new FallEventHandler(a_FallEvent);
}
/// <summary>
/// 首领举杯时的动作
/// </summary>
/// <param name="hand">若首领A左手举杯,则B攻击</param>
void a_RaiseEvent(string hand)
{
if (hand.Equals("右"))
{
Attack();
}
}
/// <summary>
/// 首领摔杯时的动作
/// </summary>
void a_FallEvent()
{
Attack();
}
/// <summary>
/// 攻击
/// </summary>
public void Attack()
{
Console.WriteLine("部下C发起攻击");
}
}
转载,做了部分优化。