C#語法學習委托(delegate)
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
* 委托(delegate)
委托聲明定義了一种類型,它用一組特定的參數以及返回類型來封裝方法.
對於靜態方法,委托對像封裝要調用的方法.
對於實例方法,委托對像同時封裝一個實例和該實例上的一個方法.
如果您有一個委托對像和一組適當的參數,則可以用這些參數調用該委托.
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
//要注意的是委托的返回值類型必須和代理的方法一致
//下面的zsEat() 是一個void無返回值類型,所以委托也必需是一個void類型.
//委托的參數也必需和代理的方法一致,參數的個數以及類型都必需一致.
class Mydelegate
{
static void zsEat(string food)
{
Console.WriteLine("張三吃" + food);
}
static void lsEat(string food)
{
Console.WriteLine("李四吃" + food);
}
static void wwEat(string food)
{
Console.WriteLine("王五吃" + food);
}
static void Main()
{
zsEat("西瓜----直接調用");//直接調用
Eatdelegate zs=new Eatdelegate(zsEat);//此處()內的字符必需有代理的方法名稱一致.
Eatdelegate ls=new Eatdelegate(lsEat);
Eatdelegate ww=new Eatdelegate(wwEat);
zs("西瓜----通過委托調用");//通過委托調用
ls("西瓜----通過委托調用");
ww("西瓜----通過委托調用");
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
* 委托(delegate)
委托聲明定義了一种類型,它用一組特定的參數以及返回類型來封裝方法.
對於靜態方法,委托對像封裝要調用的方法.
對於實例方法,委托對像同時封裝一個實例和該實例上的一個方法.
如果您有一個委托對像和一組適當的參數,則可以用這些參數調用該委托.
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
//要注意的是委托的返回值類型必須和代理的方法一致
//下面的zsEat() 是一個void無返回值類型,所以委托也必需是一個void類型.
//委托的參數也必需和代理的方法一致,參數的個數以及類型都必需一致.
class Mydelegate
{
static void zsEat(string food)
{
Console.WriteLine("張三吃" + food);
}
static void lsEat(string food)
{
Console.WriteLine("李四吃" + food);
}
static void wwEat(string food)
{
Console.WriteLine("王五吃" + food);
}
static void Main()
{
zsEat("西瓜----直接調用");//直接調用
Eatdelegate zs=new Eatdelegate(zsEat);//此處()內的字符必需有代理的方法名稱一致.
Eatdelegate ls=new Eatdelegate(lsEat);
Eatdelegate ww=new Eatdelegate(wwEat);
zs("西瓜----通過委托調用");//通過委托調用
ls("西瓜----通過委托調用");
ww("西瓜----通過委托調用");
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:37
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
*上個例子中我們調用了三次委托的方法,這樣感覺上挺麻煩的這節課講一下委托鏈
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
//要注意的是委托的返回值類型必須和代理的方法一致
//下面的zsEat() 是一個void無返回值類型,所以委托也必需是一個void類型.
//委托的參數也必需和代理的方法一致,參數的個數以及類型都必需一致.
class Mydelegate
{
static void zsEat(string food)
{
Console.WriteLine("張三吃" + food);
}
static void lsEat(string food)
{
Console.WriteLine("李四吃" + food);
}
static void wwEat(string food)
{
Console.WriteLine("王五吃" + food);
}
static void Main()
{
zsEat("西瓜----直接調用");//直接調用
Eatdelegate zs=new Eatdelegate(zsEat);//此處()內的字符必需有代理的方法名稱一致.
Eatdelegate ls=new Eatdelegate(lsEat);
Eatdelegate ww=new Eatdelegate(wwEat);
Eatdelegate eatChain;//聲明委托鏈
eatChain=zs+ls+ww;//將各托相加,執行的順序是接相加的順序.
eatChain("西瓜----通過委托調用");//執行結果和前一個例子一樣.
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:37
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
*上個例子中我們調用了三次委托的方法,這樣感覺上挺麻煩的這節課講一下委托鏈
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
//要注意的是委托的返回值類型必須和代理的方法一致
//下面的zsEat() 是一個void無返回值類型,所以委托也必需是一個void類型.
//委托的參數也必需和代理的方法一致,參數的個數以及類型都必需一致.
class Mydelegate
{
static void zsEat(string food)
{
Console.WriteLine("張三吃" + food);
}
static void lsEat(string food)
{
Console.WriteLine("李四吃" + food);
}
static void wwEat(string food)
{
Console.WriteLine("王五吃" + food);
}
static void Main()
{
zsEat("西瓜----直接調用");//直接調用
Eatdelegate zs=new Eatdelegate(zsEat);//此處()內的字符必需有代理的方法名稱一致.
Eatdelegate ls=new Eatdelegate(lsEat);
Eatdelegate ww=new Eatdelegate(wwEat);
Eatdelegate eatChain;//聲明委托鏈
eatChain=zs+ls+ww;//將各托相加,執行的順序是接相加的順序.
eatChain("西瓜----通過委托調用");//執行結果和前一個例子一樣.
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
*上個例子中我們調用了三次委托的方法,這樣感覺上挺麻煩的這節課講一下委托鏈
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
//要注意的是委托的返回值類型必須和代理的方法一致
//下面的zsEat() 是一個void無返回值類型,所以委托也必需是一個void類型.
//委托的參數也必需和代理的方法一致,參數的個數以及類型都必需一致.
class Mydelegate
{
static void zsEat(string food)
{
Console.WriteLine("張三吃" + food);
}
static void lsEat(string food)
{
Console.WriteLine("李四吃" + food);
}
static void wwEat(string food)
{
Console.WriteLine("王五吃" + food);
}
static void Main()
{
zsEat("西瓜----直接調用");//直接調用
Eatdelegate zs=new Eatdelegate(zsEat);//此處()內的字符必需有代理的方法名稱一致.
Eatdelegate ls=new Eatdelegate(lsEat);
Eatdelegate ww=new Eatdelegate(wwEat);
Eatdelegate eatChain;//聲明委托鏈
Console.WriteLine("張三,李四,王五開座談會.");
eatChain=zs+ls+ww;//將各托相加,執行的順序是接相加的順序.
eatChain("西瓜----通過委托調用");//執行結果和前一個例子一樣.
Console.WriteLine("李四接了個電話");
eatChain-=ls;
eatChain("香蕉");
Console.WriteLine("李四回來了");
eatChain+=ls;//此處再把ls加進來後李四就變成最後一個執行的方法了.
eatChain("桔子");
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
*上個例子中我們調用了三次委托的方法,這樣感覺上挺麻煩的這節課講一下委托鏈
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
//要注意的是委托的返回值類型必須和代理的方法一致
//下面的zsEat() 是一個void無返回值類型,所以委托也必需是一個void類型.
//委托的參數也必需和代理的方法一致,參數的個數以及類型都必需一致.
class Mydelegate
{
static void zsEat(string food)
{
Console.WriteLine("張三吃" + food);
}
static void lsEat(string food)
{
Console.WriteLine("李四吃" + food);
}
static void wwEat(string food)
{
Console.WriteLine("王五吃" + food);
}
static void Main()
{
zsEat("西瓜----直接調用");//直接調用
Eatdelegate zs=new Eatdelegate(zsEat);//此處()內的字符必需有代理的方法名稱一致.
Eatdelegate ls=new Eatdelegate(lsEat);
Eatdelegate ww=new Eatdelegate(wwEat);
Eatdelegate eatChain;//聲明委托鏈
Console.WriteLine("張三,李四,王五開座談會.");
eatChain=zs+ls+ww;//將各托相加,執行的順序是接相加的順序.
eatChain("西瓜----通過委托調用");//執行結果和前一個例子一樣.
Console.WriteLine("李四接了個電話");
eatChain-=ls;
eatChain("香蕉");
Console.WriteLine("李四回來了");
eatChain+=ls;//此處再把ls加進來後李四就變成最後一個執行的方法了.
eatChain("桔子");
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
*上個例子中我們調用了三次委托的方法,這樣感覺上挺麻煩的這節課講一下委托鏈
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
//要注意的是委托的返回值類型必須和代理的方法一致
//下面的zsEat() 是一個void無返回值類型,所以委托也必需是一個void類型.
//委托的參數也必需和代理的方法一致,參數的個數以及類型都必需一致.
class Mydelegate
{
static void Main()
{
Eatdelegate eatChain=null;//聲明委托鏈
//下面是匿名方法的介紹
//webcast中,李建忠"C#2.0銳利體驗系列課程"中有一節"匿名方法,迭代器"的講課.
eatChain+=delegate(string food){Console.WriteLine("張三吃" + food);};
eatChain+=delegate(string food){Console.WriteLine("李四吃" + food);};
eatChain+=delegate(string food){Console.WriteLine("王五吃" + food);};
eatChain("西瓜----通過委托調用");//執行結果和前一個例子一樣.
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
*上個例子中我們調用了三次委托的方法,這樣感覺上挺麻煩的這節課講一下委托鏈
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
//要注意的是委托的返回值類型必須和代理的方法一致
//下面的zsEat() 是一個void無返回值類型,所以委托也必需是一個void類型.
//委托的參數也必需和代理的方法一致,參數的個數以及類型都必需一致.
class Mydelegate
{
static void Main()
{
Eatdelegate eatChain=null;//聲明委托鏈
//下面是匿名方法的介紹
//webcast中,李建忠"C#2.0銳利體驗系列課程"中有一節"匿名方法,迭代器"的講課.
eatChain+=delegate(string food){Console.WriteLine("張三吃" + food);};
eatChain+=delegate(string food){Console.WriteLine("李四吃" + food);};
eatChain+=delegate(string food){Console.WriteLine("王五吃" + food);};
eatChain("西瓜----通過委托調用");//執行結果和前一個例子一樣.
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
*前幾個例子都是對靜態方法的委托,下面的例子是對動態方法的委托
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
class Man
{
private string name;
public Man(string name_)
{
name=name_;
//or 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);
Eatdelegate eatChain;//聲明委托鏈
Console.WriteLine("張三,李四,王五開座談會.");
eatChain=zs+ls+ww;//將各托相加,執行的順序是接相加的順序.
eatChain("西瓜----通過委托調用");//執行結果和前一個例子一樣.
Console.WriteLine("李四接了個電話");
eatChain-=ls;
eatChain("香蕉");
Console.WriteLine("李四回來了");
eatChain+=ls;//此處再把ls加進來後李四就變成最後一個執行的方法了.
eatChain("桔子");
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
*前幾個例子都是對靜態方法的委托,下面的例子是對動態方法的委托
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
class Man
{
private string name;
public Man(string name_)
{
name=name_;
//or 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);
Eatdelegate eatChain;//聲明委托鏈
Console.WriteLine("張三,李四,王五開座談會.");
eatChain=zs+ls+ww;//將各托相加,執行的順序是接相加的順序.
eatChain("西瓜----通過委托調用");//執行結果和前一個例子一樣.
Console.WriteLine("李四接了個電話");
eatChain-=ls;
eatChain("香蕉");
Console.WriteLine("李四回來了");
eatChain+=ls;//此處再把ls加進來後李四就變成最後一個執行的方法了.
eatChain("桔子");
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
*下面介紹的是如何把委托做為一個方法傳遞,並進行動態的調用
* webcast 俞暉的"Modern C#系統課程"的"深入委托和事件"
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
class Man
{
private string name;
public Man(string name_)
{
name=name_;
//or this.name=name_;
}
public void Eat(string food)
{
Console.WriteLine(name + "吃" + food);
}
}
class Party
{
static void eatTogther(string food,params Eatdelegate[] values)
{
if(values==null)
{
Console.WriteLine("座談會結束了");
}
else
{
Eatdelegate eatChain=null;
foreach(Eatdelegate ed in values)
eatChain+=ed;
eatChain(food);
Console.WriteLine();
}
}
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("張三,李四,王五開座談會.");
eatTogther("西瓜",zs,ls,ww);
Console.WriteLine("李四接了個電話");
eatTogther("香蕉",zs,ww);
Console.WriteLine("李四回來了");
eatTogther("桔子",zs,ls,ww);
eatTogther(null,null);
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/9/2
* Time: 下午 10:23
*下面介紹的是如何把委托做為一個方法傳遞,並進行動態的調用
* webcast 俞暉的"Modern C#系統課程"的"深入委托和事件"
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
delegate void Eatdelegate(string food);
class Man
{
private string name;
public Man(string name_)
{
name=name_;
//or this.name=name_;
}
public void Eat(string food)
{
Console.WriteLine(name + "吃" + food);
}
}
class Party
{
static void eatTogther(string food,params Eatdelegate[] values)
{
if(values==null)
{
Console.WriteLine("座談會結束了");
}
else
{
Eatdelegate eatChain=null;
foreach(Eatdelegate ed in values)
eatChain+=ed;
eatChain(food);
Console.WriteLine();
}
}
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("張三,李四,王五開座談會.");
eatTogther("西瓜",zs,ls,ww);
Console.WriteLine("李四接了個電話");
eatTogther("香蕉",zs,ww);
Console.WriteLine("李四回來了");
eatTogther("桔子",zs,ls,ww);
eatTogther(null,null);
}
}
using System;
public delegate void GreetingDelegate(string name);
class Test
{
private static void GreetPepole(string name,GreetingDelegate MakeGreeting)
{
MakeGreeting(name);
}
private static void EnglishGreeting(string name)
{
Console.WriteLine("Hello," + name);
}
private static void ChineseGreeting(string name)
{
Console.WriteLine("你好," + name);
}
static void Main()
{
/*
GreetPepole("Athrun",EnglishGreeting);
GreetPepole("張學友",ChineseGreeting);
//*/
GreetingDelegate d=new GreetingDelegate(EnglishGreeting);
d=ChineseGreeting;
d+=EnglishGreeting;
d+=ChineseGreeting;
d("張學友");
}
}
public delegate void GreetingDelegate(string name);
class Test
{
private static void GreetPepole(string name,GreetingDelegate MakeGreeting)
{
MakeGreeting(name);
}
private static void EnglishGreeting(string name)
{
Console.WriteLine("Hello," + name);
}
private static void ChineseGreeting(string name)
{
Console.WriteLine("你好," + name);
}
static void Main()
{
/*
GreetPepole("Athrun",EnglishGreeting);
GreetPepole("張學友",ChineseGreeting);
//*/
GreetingDelegate d=new GreetingDelegate(EnglishGreeting);
d=ChineseGreeting;
d+=EnglishGreeting;
d+=ChineseGreeting;
d("張學友");
}
}
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。