在c#中使用一個類時,分兩個階段。首先需要定義這個類,即告訴編譯器這個類由什么字段和方法組成。然后(除非只使用靜態方法)實例化類的一個對象。使用委托時,也需要經過這兩個步驟。首先定義要使用的委托,對于委托,定義它就是告訴編譯器這種類型代表了那種類型的方法,然后創建該委托的一個或多個實例。
定義委托是從delegate開始的然而它是如何運作的呢。也許弄個鼠標事件會容易理解一些,這里還是拿出書中的例子來。
using system;
namespace wrox.profcsharp.advancedcsharp
{
delegate bool compareop(object lhs, object rhs);
class mainentrypoint
{
static void main()
{
employee [] employees =
{
new employee("karli watson", 20000),
new employee("bill gates", 10000),
new employee("simon robinson", 25000),
new employee("mortimer", (decimal)1000000.38),
new employee("arabel jones", 23000),
new employee("avon from 'blake's 7'", 50000)};
compareop employeecompareop = new compareop(employee.rhsisgreater);
bubblesorter.sort(employees, employeecompareop);
for (int i=0 ; i<employees.length ; i++)
console.writeline(employees[i].tostring());
console.readline();
}
}
class employee // : object
{
private string name;
private decimal salary;
public employee(string name, decimal salary)
{
this.name = name;
this.salary = salary;
}
public override string tostring()
{
return string.format(name + ", {0:c}", salary);
}
public static bool rhsisgreater(object lhs, object rhs)
{
employee emplhs = (employee) lhs;
employee emprhs = (employee) rhs;
return (emprhs.salary > emplhs.salary) ? true : false;
}
}
class bubblesorter
{
static public void sort(object [] sortarray, compareop gtmethod)
{
for (int i=0 ; i<sortarray.length ; i++)
{
for (int j=i+1 ; j<sortarray.length ; j++)
{
if (gtmethod(sortarray[j], sortarray[i]))
{
object temp = sortarray[i];
sortarray[i] = sortarray[j];
sortarray[j] = temp;
}
}
}
}
}
}
using system;
namespace wrox.profcsharp.advancedcsharp
{
delegate bool compareop(object lhs, object rhs);
class mainentrypoint
{
static void main()
{
employee [] employees =
{
new employee("karli watson", 20000),
new employee("bill gates", 10000),
new employee("simon robinson", 25000),
new employee("mortimer", (decimal)1000000.38),
new employee("arabel jones", 23000),
new employee("avon from 'blake's 7'", 50000)};
compareop employeecompareop = new compareop(employee.rhsisgreater);
bubblesorter.sort(employees, employeecompareop);
for (int i=0 ; i<employees.length ; i++)
console.writeline(employees[i].tostring());
console.readline();
}
}
class employee // : object
{
private string name;
private decimal salary;
public employee(string name, decimal salary)
{
this.name = name;
this.salary = salary;
}
public override string tostring()
{
return string.format(name + ", {0:c}", salary);
}
public static bool rhsisgreater(object lhs, object rhs)
{
employee emplhs = (employee) lhs;
employee emprhs = (employee) rhs;
return (emprhs.salary > emplhs.salary) ? true : false;
}
}
class bubblesorter
{
static public void sort(object [] sortarray, compareop gtmethod)
{
for (int i=0 ; i<sortarray.length ; i++)
{
for (int j=i+1 ; j<sortarray.length ; j++)
{
if (gtmethod(sortarray[j], sortarray[i]))
{
object temp = sortarray[i];
sortarray[i] = sortarray[j];
sortarray[j] = temp;
}
}
}
}
}
}
代碼中,首先聲明了一個delegate bool compareop(object lhs, object rhs)委托,再說說委托:
委托機制是促使事件發送與事件接受的一種對接策略,對象對周圍信號的反應或在一定環境中所具備的對其它對象的通知行為的響應則被描述成所謂的“事件”,這可以類比人對周圍世界反饋產生信號的能力。委托就是一種定向信號流:指定產生、接受信號者并產生信號反饋的技術。那么這段委托是怎么把程序連動起來的呢,看后面的代碼。
首先是compareop employeecompareop = new compareop(employee.rhsisgreater)這里就象定義了一個監聽裝置,一旦發生了compareop(object lhs, object rhs)這個事件就去執行employee.rhsisgreater的代碼。
接下來我們就去看看employee.rhsisgreater里面的東西。
public static bool rhsisgreater(object lhs, object rhs)
{
employee emplhs = (employee) lhs;
employee emprhs = (employee) rhs;
return (emprhs.salary > emplhs.salary) ? true : false;
}
也就是說rhsisgreater的參數是匹配compareop存在的,參數中沒有直接使用employee這個type而是采用了一種通用的做法,全都弄成object需要的時候再做轉換,暫時還無法理解其深遠的意義,先記著了。估計是定義委托時不能用這些自定義的type吧。
那么這段代碼是什么時候運行的呢,注意看這段
static public void sort(object [] sortarray, compareop gtmethod)
{
for (int i=0 ; i<sortarray.length ; i++)
{
for (int j=i+1 ; j<sortarray.length ; j++)
{
if (gtmethod(sortarray[j], sortarray[i]))
{
object temp = sortarray[i];
sortarray[i] = sortarray[j];
sortarray[j] = temp;
}
}
}
}
其中static public void sort(object [] sortarray, compareop gtmethod)的參數里就有這種我們委托好的compareop了。也就是說一旦運行到if (gtmethod(sortarray[j], sortarray[i]))系統就會去找compareop employeecompareop = new compareop(employee.rhsisgreater);然后找public static bool rhsisgreater(object lhs, object rhs)這樣就執行到里面的代碼了。
整個事件響應完成。
新聞熱點
疑難解答