C# 委托
|
admin
2017年3月8日 0:16
本文熱度 6253
|
C#中的委托類似于C++中的函數(shù)指針.可以根據(jù)定義的委托實現(xiàn)不同的函數(shù).定義委托的方法如下
- [修飾符]delegate 返回類型 委托名([參數(shù)列表])
其中修飾符合參數(shù)列表是可選項.使用的時候先聲明一個委托.例如:定義一個返回類型為void的,需要一個變量的委托
- delegate void MyDelegate(object o1)
然后在定義需要使用該委托的方法,這個方法必須和聲明的委托具有相同的簽名,即返回類型相同,而且需要傳遞一個參數(shù).
- private void Myfunction1(object o1)
- {
- string str = (string)o1;
-
- }
-
- private void Myfunction2(object o2)
- {
- int num = (int)o2;
-
- }
使用方法:
MyDelegate mydelegate1 = new MyDelegate (Myfunction1);
MyDelegate mydelegate2 = new MyDelegate (Myfunction2);
mydelegate1 ("This is a demon!");
mydelegate2 (25);
委托在使用的時候必須要實例化,這樣就實現(xiàn)了一個委托實例化不同的函數(shù)。
附上兩個實例:
第一個:一個雇員類,實現(xiàn)按年齡和薪金排序
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace 委托
- {
-
-
-
-
-
-
-
- delegate bool Compare(object o1, object o2);
- class Employee
- {
- private int mAge;
- private int mSalary;
- private string mName;
-
- public Employee(string aName, int aAge, int aSalary)
- {
- this.mAge = aAge;
- this.mSalary = aSalary;
- this.mName = aName;
- }
- public void Print()
- {
- Console.WriteLine("Name is:{0}, Age is:{1}, Salary is:{2}", mName, mAge, mSalary);
- }
- static public bool CompareAge(object o1, object o2)
- {
- Employee e1 = (Employee)o1;
- Employee e2 = (Employee)o2;
- return (e1.mAge > e2.mAge) ? true : false;
- }
- static public bool CompareSalary(object o1, object o2)
- {
- Employee e1 = (Employee)o1;
- Employee e2 = (Employee)o2;
- return (e1.mSalary > e2.mSalary) ? true : false;
- }
- }
-
- class Test
- {
- static public void Sort(object[] aSortArry, Compare CompareMethod)
- {
- for (int i=0; i<aSortArry.Length; i++)
- {
- for (int j = i + 1; j < aSortArry.Length; j++)
- {
- if (CompareMethod(aSortArry[i], aSortArry[j]))
- {
- object temp = aSortArry[i];
- aSortArry[i] = aSortArry[j];
- aSortArry[j] = temp;
- }
- }
- }
- }
- static void Main(string[] args)
- {
- Employee[] employee = {
- new Employee("Wang",12,800), new Employee("Liu", 21, 900), new Employee("Li", 34, 300),
- new Employee("Sun", 9, 1200), new Employee("Xu", 23, 2000),new Employee("Wu", 21, 400)};
- Compare CompareAge = new Compare(Employee.CompareAge);
- Compare CompareSalary = new Compare(Employee.CompareSalary);
- Console.WriteLine("Sorted by age:");
- Sort(employee, CompareAge);
- for (int i = 0; i < employee.Length; i++)
- employee[i].Print();
- Console.WriteLine("Sorted by salary:");
- Sort(employee, CompareSalary);
- for (int i = 0; i < employee.Length; i++)
- employee[i].Print();
- Console.ReadKey();
- }
- }
-
-
- }
第二個:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace 委托
- {
-
- delegate void None();
- delegate void Single(object o1);
- delegate void Complex(object o1, object o2);
- class Test
- {
- static private void DeleNone()
- {
- Console.WriteLine("This is the none delegate!");
- }
- static private void DeleSingle(object o1)
- {
- string str = (string)o1;
- Console.WriteLine(str);
- }
- static private void DeleComplex(object o1, object o2)
- {
- string str1 = (string)o1;
- string str2 = (string)o2;
- Console.WriteLine(str1 + str2);
- }
- static void Main(string[] args)
- {
- None mNone = new None(DeleNone);
- Single mSingle = new Single(DeleSingle);
- Complex mComplex = new Complex(DeleComplex);
- mSingle("This is the Single delegate!");
- mComplex("I am o1 ", "I am o2");
- Console.ReadKey();
- }
- }
- }
該文章在 2017/3/8 0:16:58 編輯過
|
|