//委托代理动态方法
using System;
delegate void Eatdelegate(string food);
class Man
{
private string name;
public Man(string name)
{
this.name=name;
}
public void eat(string food)
{
Console.WriteLine(name+"吃"+food);
}
}
class Party
{
static void Main()
{
Man ZS=new Man("张三");
Man LS=new Man("李四");
Man WW=new Man("王五");
Eatdelegate zs=new Eatdelegate(ZS.eat);
Eatdelegate ls=new Eatdelegate(LS.eat);
Eatdelegate ww=new Eatdelegate(WW.eat);
Console.WriteLine("张三,李四,王五开坐谈会");
Eatdelegate eatChain;//声明一个委托链,不需要对它进行实例化
eatChain=zs+ls+ww;//把zs,ls,ww这几个委托实例加到eatChain委托链里
eatChain("西瓜");//调用委托链,按照以上+的先后顺序去调用以上的方法
Console.WriteLine("李四出去接电话");
eatChain-=ls;//通过-=把李四开除出这个委托链
eatChain("香蕉");
Console.WriteLine("李四回来了");
eatChain+=ls;//通过+=把李四加入到这个委托链
eatChain("苹果");
}
}