作者在 2008-04-27 15:33:34 发布以下内容
using System;
delegate void delegateEat(string food);
class man
{
private string peop;
public man(string peop)
{
this.peop=peop;
}
public void maneat(string food)
{
Console.WriteLine(peop+"吃 "+food);
}
}
class test
{ //params 多个参数/在此为delegateEat委托数组
static void togetheat(string food,params delegateEat[] values)
{
if(values==null)//没人就结束
{
Console.WriteLine("game over");
}
else
{
delegateEat eatChain=null;
foreach(delegateEat r in values)
eatChain+=r;
eatChain(food);//委托链,调用man类里的maneat(sting food)方法
Console.WriteLine();
}
}
static void Main()
{
man Zs=new man("张三");//不能通过Zs.man("张三");把他初始化,
man Ls=new man("李四");//因为man类的初始化man函数没有0个参数的重载。
man Ww=new man("王五");
delegateEat zs=new delegateEat(Zs.maneat);
delegateEat ls=new delegateEat(Ls.maneat);
delegateEat ww=new delegateEat(Ww.maneat);
togetheat("西瓜",zs,ls,ww);//把委托作为方法进行传送,进行动态的调用。
Console.WriteLine("张三离开");
togetheat("苹果",ls,ww);
Console.WriteLine("张三回来");
togetheat("香蕉",ls,ww,zs);
Console.WriteLine("同名的王五过来了");
togetheat("香蕉",ls,ww,zs,ww);
Console.WriteLine("人走光了,没人吃东西了");
togetheat(null,null);
}
}
delegate void delegateEat(string food);
class man
{
private string peop;
public man(string peop)
{
this.peop=peop;
}
public void maneat(string food)
{
Console.WriteLine(peop+"吃 "+food);
}
}
class test
{ //params 多个参数/在此为delegateEat委托数组
static void togetheat(string food,params delegateEat[] values)
{
if(values==null)//没人就结束
{
Console.WriteLine("game over");
}
else
{
delegateEat eatChain=null;
foreach(delegateEat r in values)
eatChain+=r;
eatChain(food);//委托链,调用man类里的maneat(sting food)方法
Console.WriteLine();
}
}
static void Main()
{
man Zs=new man("张三");//不能通过Zs.man("张三");把他初始化,
man Ls=new man("李四");//因为man类的初始化man函数没有0个参数的重载。
man Ww=new man("王五");
delegateEat zs=new delegateEat(Zs.maneat);
delegateEat ls=new delegateEat(Ls.maneat);
delegateEat ww=new delegateEat(Ww.maneat);
togetheat("西瓜",zs,ls,ww);//把委托作为方法进行传送,进行动态的调用。
Console.WriteLine("张三离开");
togetheat("苹果",ls,ww);
Console.WriteLine("张三回来");
togetheat("香蕉",ls,ww,zs);
Console.WriteLine("同名的王五过来了");
togetheat("香蕉",ls,ww,zs,ww);
Console.WriteLine("人走光了,没人吃东西了");
togetheat(null,null);
}
}