abstract:1.委托相信大家在現(xiàn)實生活中,經(jīng)常聽到“委托”這個詞匯,尤其在涉及官司的時候,經(jīng)常聽到“委托律師”等這樣的字眼。所以,單純就”委托“這個詞而言,指的就是命令,發(fā)命令的人自己不做事,而讓委托的對象去做事。同理,在程序里面,委托只是存儲了各個方法的地址,它自己本身其實是什么都不做的。委托是一個類,它定義了方法的類型,使得可以將方法當作另一個方法的參數(shù)來進行傳遞,這種將方法動態(tài)地賦給參數(shù)的做法,可以避免
1.委托
相信大家在現(xiàn)實生活中,經(jīng)常聽到“委托”這個詞匯,尤其在涉及官司的時候,經(jīng)常聽到“委托律師”等這樣的字眼。所以,單純就”委托“這個詞而言,指的就是命令,發(fā)命令的人自己不做事,而讓委托的對象去做事。同理,在程序里面,委托只是存儲了各個方法的地址,它自己本身其實是什么都不做的。
委托是一個類,它定義了方法的類型,使得可以將方法當作另一個方法的參數(shù)來進行傳遞,這種將方法動態(tài)地賦給參數(shù)的做法,可以避免在程序中大量使用If-Else(Switch)語句,同時使得程序具有更好的可擴展性。
private delegate void GreetingPeopleDelegate(); static void Main(string[] args) { GreetPeople(EnglishGreeting); GreetPeople(ChineseGreeting); Console.ReadLine(); } private static void GreetPeople(GreetingPeopleDelegate greetingPeopleDelegate) { greetingPeopleDelegate(); } private static void EnglishGreeting() { Console.WriteLine("Good morning!"); } private static void ChineseGreeting() { Console.WriteLine("早上好!"); }
輸出結(jié)果:
Good morning!
早上好!
從上面的例子可以看出,委托可以做到把函數(shù)當作函數(shù)參數(shù)一樣傳遞,即:可以將業(yè)務(wù)邏輯作為參數(shù)傳遞,極大提高了函數(shù)的通用性。
*擴展:多播委托
使用委托可以將多個方法綁定到同一個委托變量,當調(diào)用此變量時(這里用“調(diào)用”這個詞,是因為此變量代表一個方法),可以依次調(diào)用所有綁定的方法。
現(xiàn)在我們舉個有趣的例子,讓大家更好理解多播委托的含義。
故事背景:在某次戰(zhàn)役中,主公要求指揮官諸葛亮要指揮自己的部下關(guān)羽和張飛去攻打曹軍中軍大帳,而同時要指揮趙子龍去后方偷襲糧草大營。那么這個簡單的故事,如何用程序描述呢?
首先,故事中的委托,并非單一的,而是好幾件事都需要委托,于是這里有一個新的概念——多播委托。
建立General(將軍)類
public class General { private string _name; public General(string name) { this._name = name; } public string Name { get { return this._name; } } public virtual void Execute() { Console.WriteLine("I have nothing to do yet."); } }
2. 建立Attacker(攻擊者)類,繼承General類
public class Attacker : General { public Attacker(string name) : base(name) { } public override void Execute() { Console.WriteLine(string.Format("{0} is attacking the enemy!", this.Name)); } }
3. 建立Intercepter(偷襲者)類
public class Intercepter : General { public Intercepter(string name) : base(name) { } public override void Execute() { Console.WriteLine(string.Format("{0} is intercepting the reinforcement!", this.Name)); } }
4. 建立Commander(指揮官)類
public class Commander : General { private delegate void MilitaryPlan(); public Commander(string name) : base(name) { } public void MakeMilitaryPlan(string[] attackerNames, string[] intercepterNames) { MilitaryPlan militaryPlan = new MilitaryPlan(this.AcceptLordInstructions); foreach (string attackerName in attackerNames) { Attacker attacker = new Attacker(attackerName); militaryPlan += attacker.Execute; } foreach (string intercepterName in intercepterNames) { Intercepter intercepter = new Intercepter(intercepterName); militaryPlan += intercepter.Execute; } militaryPlan(); }
調(diào)用方法:
static void Main(string[] args) { Commander commander = new Commander("諸葛亮"); commander.MakeMilitaryPlan(new string[] { "關(guān)羽", "張飛" }, new string[] { "趙云" }); Console.ReadLine(); }
運行結(jié)果:
諸葛亮 accepted the instructions from lord
關(guān)羽 is attacking the enemy!
張飛 is attacking the enemy!
趙云 is intercepting the reinforcement!
擴展: 假設(shè)主公要求的指揮官不是諸葛亮,而是鳳雛龐統(tǒng),龐統(tǒng)下達的命令是讓馬超和黃忠去襲擊曹軍中軍大帳,而讓魏延去后方偷襲糧草大營,只需要改一下傳參即可:
static void Main(string[] args) { Commander commander = new Commander("龐統(tǒng)"); commander.MakeMilitaryPlan(new string[] { "馬超", "黃忠" }, new string[] { "魏延" }); Console.ReadLine(); }
運行結(jié)果:
龐統(tǒng) accepted the instructions from lord
馬超 is attacking the enemy!
黃忠 is attacking the enemy!
魏延 is intercepting the reinforcement!