/*委托這個相對較復雜,准備考慮實用反射還是嵌套代理實現實例化人也一起做*/
using System;
using System.Collections.Generic;
using System.Text;
namespace Delegate
{
delegate void EatDelegate(string AFood);
class Program
{
static void Main(string[] args)
{
Man mZS = new Man("張三"); //實例一個對象
Man mLS = new Man("李四");
EatDelegate deleZS = new EatDelegate(mZS.Eat); //創建一個代理
EatDelegate deleLS = new EatDelegate(mLS.Eat);
EatToghter("香蕉", deleZS,deleLS);
Console.ReadLine();
}
//用委托作為參數傳遞
static void EatToghter(string AFood, params EatDelegate[] AParams)
{
if (AParams == null)
{
Console.WriteLine("座談會結束!");
}
else
{
EatDelegate deleEatChain = null;
foreach (EatDelegate deleEat in AParams)
{
deleEatChain += deleEat;
}
deleEatChain(AFood);
}
}
}
//創建一個對象類封裝屬性和方法
class Man
{
private string sName;
public Man(string AName)
{
this.sName = AName;
}
public void Eat(string AFood)
{
Console.WriteLine(sName + "在吃" + AFood);
}
}
}