Csharp关键字----delegate(委托)
Delegate類簡介
------------------------?????
命名空間:System
程序集:mscorlib(在 mscorlib.dll 中)
?? 委托(Delegate)類是一種數據結構,通過它可引用靜態方法或引用類實例及該類的實例方法。
以往的界面編程中我們應該都接觸過各種類型的事件驅動(event driven)的處理模式,
在這種模式里,我們定義相應事件觸發的函數。
例如:
Button1 的 Click事件,我們可以編寫Button1_Click 或 Btn1Clicked等函數來做相應的驅動處理。
而事件與驅動函數的對應關系就是通過委托(Delegate)類來關聯的。
其實委托(Delegate)類這種數據結構有些類似于之前C/C++中的函數指針。
Delegate類一個簡單應用
------------------------????
1.定義一個Delegate函數數據結構
2.定義Delegate將引用的靜態方法或引用類實例及該類的實例方法
3.Delegate數據變量指向實例方法
4.通過Delegate數據變量執行實例方法
A very basic example (TestClass.cs):
1: using System; 2: 3: namespace MySample 4: { 5: class TestClass 6: { 7: //1.定義一個Delegate函數數據結構 8: public delegate void GoDelegate(); 9: 10: [STAThread] 11: static void Main(string[] args) 12: { 13: //3.Delegate數據變量指向實例方法 14: GoDelegate goDelegate = new GoDelegate( MyDelegateFunc); 15: 16: //4.通過Delegate數據變量執行實例方法 17: goDelegate(); 18: return; 19: } 20: //2.定義Delegate將引用的靜態方法或引用類實例及該類的實例方法 21: public static void MyDelegateFunc() 22: { 23: Console.WriteLine("delegate function..."); 24: } 25: } 26: }編譯執行結果:
# TestClass.exe
delegate function...
使用Delegate類和Override實現多態的比較
-----------------------------------------------
1.使用Delegate類的時候,下面的例子可以很清楚的說明。
? 1.1 首先定義一個動物基類(MyAnimalDelegateClass), 基類中有顯示屬性的(ShowAnimalType)的public方法。
????? 并且在ShowAnimalType方法中調用Delegate引用的實例方法
? 1.2 定義獅子(LionDelegateClass)和馬(HorseDelegateClass)兩個子類。Delegate與各自的實例方法綁定
????? 實現不同的屬性顯示(ShowAnimalType)方法。
Delegate example (TestClass.cs):
1: AnonymousDelegatesusing System; 2: 3: namespace MySample 4: { 5: class TestClass 6: { 7: [STAThread] 8: static void Main(string[] args) 9: { 10: //獅子(LionDelegateClass)的屬性顯示(ShowAnimalType)方法調用 11: LionDelegateClass lionDelegate = new LionDelegateClass(); 12: lionDelegate.ShowAnimalType("MySample"); 13: 14: //馬(HorseDelegateClass)的屬性顯示(ShowAnimalType)方法調用 15: HorseDelegateClass horseDelegate = new HorseDelegateClass(); 16: horseDelegate.ShowAnimalType("MySample"); 17: } 18: } 19: 20: //動物基類(MyAnimalDelegateClass) 21: public class MyAnimalDelegateClass 22: { 23: //Delegate數據結構定義 24: public delegate void DelegateFunction(string strFuncName); 25: 26: private DelegateFunction m_delegateFunction = null; 27: 28: //Delegate類型的屬性 29: public DelegateFunction delegateFunction 30: { 31: get 32: { 33: return m_delegateFunction; 34: } 35: set 36: { 37: m_delegateFunction = value; 38: } 39: } 40: //屬性顯示(ShowAnimalType)方法 41: public void ShowAnimalType(string strFuncName) 42: { 43: if (delegateFunction != null) 44: { 45: object[] args = {strFuncName}; 46: //調用Delegate引用的實例方法 47: delegateFunction.DynamicInvoke(args); 48: } 49: } 50: } 51: 52: //獅子(LionDelegateClass) 53: public class LionDelegateClass:MyAnimalDelegateClass 54: { 55: public LionDelegateClass() 56: { 57: this.delegateFunction = new DelegateFunction(subFunction1); 58: } 59: 60: //獅子(LionDelegateClass)實例方法的實裝 61: private void subFunction1(string strFuncName) 62: { 63: System.Console.WriteLine( 64: string.Format("[{0}]This is a lion....", strFuncName)); 65: } 66: } 67: 68: //馬(HorseDelegateClass) 69: public class HorseDelegateClass:MyAnimalDelegateClass 70: { 71: public HorseDelegateClass() 72: { 73: this.delegateFunction = new DelegateFunction(subFunction2); 74: } 75: 76: //馬(HorseDelegateClass)實例方法的實裝 77: private void subFunction2(string strFuncName) 78: { 79: System.Console.WriteLine( 80: string.Format("[{0}]This is a horse....", strFuncName)); 81: } 82: } 83: }編譯執行結果:
# TestClass.exe
[MySample]This is a lion....
[MySample]This is a horse....
2.使用Override實裝的時候,參考下面的例子。
? 1.1 首先定義一個動物基類(AbstractAnimalNoDelegateClass), 基類中有顯示屬性的(ShowAnimalType)的public方法。
????? 并且在ShowAnimalType方法中調用抽象方法(NoDelegateFunction)
? 1.2 定義獅子(LionNoDelegateClass)和馬(HorseNoDelegateClass)兩個子類。
????? 子類中實裝抽象方法(NoDelegateFunction)
????? 實現不同的屬性顯示(ShowAnimalType)方法。
Override example (TestClass.cs):
1: using System; 2: 3: namespace MySample 4: { 5: class TestClass 6: { 7: [STAThread] 8: static void Main(string[] args) 9: { 10: //獅子(LionNoDelegateClass )的屬性顯示(ShowAnimalType)方法調用 11: LionNoDelegateClass lionNoDelegate = new LionNoDelegateClass(); 12: lionNoDelegate.ShowAnimalType("MySample"); 13: 14: //馬(HorseNoDelegateClass )的屬性顯示(ShowAnimalType)方法調用 15: HorseNoDelegateClass horseNoDelegate = new HorseNoDelegateClass(); 16: horseNoDelegate.ShowAnimalType("MySample"); 17: } 18: } 19: 20: //動物基類(AbstractAnimalNoDelegateClass) 21: public abstract class AbstractAnimalNoDelegateClass 22: { 23: public void ShowAnimalType(string strFuncName) 24: { 25: //抽象方法(NoDelegateFunction)調用 26: NoDelegateFunction(strFuncName); 27: } 28: //在基類中定義抽象方法(NoDelegateFunction) 29: protected abstract void NoDelegateFunction(string strFuncName); 30: } 31: 32: //獅子(LionNoDelegateClass ) 33: public class LionNoDelegateClass:AbstractAnimalNoDelegateClass 34: { 35: // 子類中實裝抽象方法(NoDelegateFunction) 36: protected override void NoDelegateFunction(string strFuncName) 37: { 38: System.Console.WriteLine( 39: string.Format("[{0}]This is a lion....", strFuncName)); 40: } 41: } 42: //馬(HorseNoDelegateClass ) 43: public class HorseNoDelegateClass:AbstractAnimalNoDelegateClass 44: { 45: // 子類中實裝抽象方法(NoDelegateFunction) 46: protected override void NoDelegateFunction(string strFuncName) 47: { 48: System.Console.WriteLine( 49: string.Format("[{0}]This is a horse....", strFuncName)); 50: } 51: } 52: } 53:編譯執行結果:
# TestClass.exe
[MySample]This is a lion....
[MySample]This is a horse....
3.比較Delegate和Override實裝方式
? 可以看出Delegate實裝方式中,相當于定義一個函數指針的成員變量。
? 通過把實裝函數的地址賦給該成員變量,實現同樣的方法,處理方式的不同。
? 而Override方式中,則是在父類中預先定義好接口,通過實裝的不同,
? 來實現同樣的方法,處理方式的不同。
? Delegate實裝方式比較靈活,適合設計不是很完善的場合,便于修改。
? Override方式封裝性好,相對比較安全。
MulticastDelegate 類的應用
---------------------------------
在C#中,委托(Delegate)類是多路委托,這就說可以同時指向多個處理函數,
并且可以按照委托的先后順序,執行相應的函數。
如下例:
65: }
編譯執行結果: # TestClass.exe [MySample]
This is a dog....[MySample]This is a nice dog....
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/xinsir/archive/2006/08/22/1106277.aspx
1: // Copyright ? Microsoft Corporation. All Rights Reserved. 2: // This code released under the terms of the 3: // Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) 4: // 5: //Copyright (C) Microsoft Corporation. All rights reserved. 6: 7: using System; 8: using System.Collections.Generic; 9: using System.Text; 10: 11: namespace AnonymousDelegate_Sample 12: { 13: 14: // Define the delegate method. 15: delegate decimal CalculateBonus(decimal sales); 16: 17: // Define an Employee type. 18: class Employee 19: { 20: public string name; 21: public decimal sales; 22: public decimal bonus; 23: public CalculateBonus calculation_algorithm; 24: } 25: 26: class Program 27: { 28: 29: // This class will define two delegates that perform a calculation. 30: // The first will be a named method, the second an anonymous delegate. 31: 32: // This is the named method. 33: // It defines one possible implementation of the Bonus Calculation algorithm. 34: 35: static decimal CalculateStandardBonus(decimal sales) 36: { 37: return sales / 10; 38: } 39: 40: static void Main(string[] args) 41: { 42: 43: // A value used in the calculation of the bonus. 44: // Note: This local variable will become a "captured outer variable". 45: decimal multiplier = 2; 46: 47: // This delegate is defined as a named method. 48: CalculateBonus standard_bonus = new CalculateBonus(CalculateStandardBonus); 49: 50: // This delegate is anonymous - there is no named method. 51: // It defines an alternative bonus calculation algorithm. 52: CalculateBonus enhanced_bonus = delegate(decimal sales) 53: { return multiplier * sales / 10; }; 54: 55: // Declare some Employee objects. 56: Employee[] staff = new Employee[5]; 57: 58: // Populate the array of Employees. 59: for (int i = 0; i < 5; i++) 60: staff[i] = new Employee(); 61: 62: // Assign initial values to Employees. 63: staff[0].name = "Mr Apple"; 64: staff[0].sales = 100; 65: staff[0].calculation_algorithm = standard_bonus; 66: 67: staff[1].name = "Ms Banana"; 68: staff[1].sales = 200; 69: staff[1].calculation_algorithm = standard_bonus; 70: 71: staff[2].name = "Mr Cherry"; 72: staff[2].sales = 300; 73: staff[2].calculation_algorithm = standard_bonus; 74: 75: staff[3].name = "Mr Date"; 76: staff[3].sales = 100; 77: staff[3].calculation_algorithm = enhanced_bonus; 78: 79: staff[4].name = "Ms Elderberry"; 80: staff[4].sales = 250; 81: staff[4].calculation_algorithm = enhanced_bonus; 82: 83: // Calculate bonus for all Employees 84: foreach (Employee person in staff) 85: PerformBonusCalculation(person); 86: 87: // Display the details of all Employees 88: foreach (Employee person in staff) 89: DisplayPersonDetails(person); 90: 91: 92: } 93: 94: public static void PerformBonusCalculation(Employee person) 95: { 96: 97: // This method uses the delegate stored in the person object 98: // to perform the calculation. 99: // Note: This method knows about the multiplier local variable, even though 100: // that variable is outside the scope of this method. 101: // The multipler varaible is a "captured outer variable". 102: person.bonus = person.calculation_algorithm(person.sales); 103: } 104: 105: public static void DisplayPersonDetails(Employee person) 106: { 107: Console.WriteLine(person.name); 108: Console.WriteLine(person.bonus); 109: Console.WriteLine("---------------"); 110: Console.ReadLine(); 111: } 112: } 113: }轉載于:https://www.cnblogs.com/xiuyusoft/archive/2011/06/14/2080196.html
總結
以上是生活随笔為你收集整理的Csharp关键字----delegate(委托)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 债券具有收益性、安全性、流动性,适合普通
- 下一篇: TFS2008 基本安装