c#重點知識解答(五)
2024-07-21 02:15:56
供稿:網友
 
網站運營seo文章大全提供全面的站長運營經驗及seo技術!
第五章:代理 
代理實現的是象c++等語言的指針功能,不同于函數指針,代理是一種面向對象、安全類型的。代理事派生于公共基類(system)的一種參考類型,方法被壓入一個代理中,對于實例方法被稱為實例的組成實體或關于實例的方法,而靜態方法,被稱為類的組成實體或類方法。代理的強大功能是它可以自動的匹配方法,而不管其類型。 
寫一個代理包括三個步驟: 
寫代理、實例化、調用。 
代理的聲明使用以下語法: 
delegate void simpledelegate(); 
實例化一個代理 
class test 
{ 
static void f() { 
system.console.writeline("hello world"); 
} 
static void main() { 
simpledelegate d = new simpledelegate(f);//將方法壓入 
d();//通過代理; 
f();//不通過代理; 
} 
} 
最后讓我們調用她 
void multicall(simpledelegate d, int count) { 
for (int i = 0; i < count; i++) 
d(); 
} 
} 
我們可以看到對于方法的調用是通過代理來完成的,調用時并不需要知道被調用她的類型。代理在我看來好比是對象要一件事她不直接地調用這個方法,而是通過一個中間人去調用她。 
下面就代理的強大功能進行詳細介紹:首先然我們實現一個這樣的功能,考慮一下該如何用指向基類的對象調用子類的成員函數。在這里程序員是不是點懷戀指針了,不過在c#中這樣的功能完全也可實現的,使用一個單獨的代理我們可以完成這項功能。以下代碼來自timothy a. vanover文章。 
namespace delegatescs 
{ 
using system; 
public class wisdom //包含代理的類 
{ 
public delegate string giveadvice(); 
public string offeradvice(giveadvice words) 
{ 
return words(); 
} 
} 
public class parent //基類 
{ 
public virtual string advice() 
{ 
return("listen to reason"); 
} 
~parent() {} 
} 
public class dad: parent //子類 
{ 
public dad() {} 
public override string advice() 
{ 
return("listen to your mom"); 
} 
~dad() {} 
} 
public class mom: parent //子類 
{ 
public mom() {} 
public override string advice() 
{ 
return("listen to your dad"); 
} 
~mom() {} 
} 
public class daughter //不繼承與基類的類 
{ 
public daughter() {} 
public string advice() 
{ 
return("i know all there is to life"); 
} 
~daughter() {} 
} 
public class test 
{ 
public static string calladvice(parent p)//使用基類 
{ 
wisdom parents = new wisdom(); 
wisdom.giveadvice teenagegirls = new wisdom.giveadvice(p.advice);//將advice方法委托給teenagegirls委托對象 
return(parents.offeradvice(teenagegirls)); 
} 
public static void main() 
{ 
dad d = new dad(); 
mom m = new mom(); 
daughter g = new daughter(); 
//以下兩個為衍于基類的類 
console.writeline(calladvice(d)); 
console.writeline(calladvice(m)); 
//以下為未衍于基類的類,如果調用將出錯。 
//console.writeline(calladvice(g)); 
} 
} 
} 
代理 二 
1〉事件 
上一章講解了有關代理的基本應用,本章將繼續講解深入代理的使用。這里我們講解使用代理來處理事件。關于事件在另一章進行詳細講解。處理事件在c#中對比c++和vb來說更聰明,你可以寫代理然后寫事件處理者,事件處理者是一種定義在控件和窗體類中的重載的公共事件。我們在以下的例子中將看到代理在事件中的應用。 
1。寫代理 
我想處理鼠標單擊事件和在鼠標單擊左鍵或右鍵處理一些代碼。寫下面的代碼在你的初始控件函數中。 
this.mousedown += new system.winforms.mouseeventhandler(this.form_mousedown); 
2. 寫事件 
現在你可以寫事件處理,你的事件的輸出參數將返回窗體的鼠標事件參數的詳細內容。以下時鼠標事件參數成員 
mouseeventargs members 
button 指示哪一個鍵被壓,分為左、右、中、無 。 
clicks 指示鼠標壓下次數及釋放狀態。 
delta 指示鼠標轉動數量計數 
x 鼠標點擊x坐標點 
y 鼠標點擊y坐標點 
event handler 
private void form_mousedown(object sender, system.winforms.mouseeventargs e) 
{ 
switch (e.button) 
{ 
case mousebuttons.left: 
messagebox.show(this,"left button click"); 
break; 
case mousebuttons.right: 
messagebox.show(this,"right button click" ); 
break; 
case mousebuttons.middle: 
break; 
default: 
break; 
} 
} 
在你的winform中測試你的程序,你會看到通過代理事件被關聯了。 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
class i 
{ 
public i(){} 
~i() {} 
public void idoloveyou() 
{system.console.writeline("i do love you");} 
public void why(){system.console.writeline("why?");} 
} 
class her 
{ 
public her(){} 
~her() {} 
public void ido() 
{system.console.writeline("...............");} 
public void slient(){system.console.writeline(".........");} 
} 
class telephone 
{public delegate void heartchat(); 
public telephone(){} 
~telephone(){} 
public void hello(){system.console.writeline("yesterday night,i telephone to my girlfriend"); } 
} 
class chat{ 
static void main() { 
i i=new i(); 
her her=new her(); 
telephone telephone =new telephone(); 
telephone.hello(); 
telephone.heartchat tell=new telephone.heartchat(i.idoloveyou); 
tell(); 
telephone.heartchat answer=new telephone.heartchat(her.ido); 
answer(); 
telephone.heartchat ask=new telephone.heartchat(i.why); 
ask(); 
telephone.heartchat noanswer=new telephone.heartchat(her.slient); 
noanswer(); 
} 
}