在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
1<employees.length ",="" (emprhs.salary="" +="" :="" ;="" bool="" class="" console.readline();="" console.writeline(employees[i].tostring());="" decimal="" emplhs="(Employee)" employee="" employee(string="" emprhs="(Employee)" i++)="" lhs,="" lhs;="" name,="" name;="" object="" override="" private="" public="" return="" rhs)="" rhs;="" rhsisgreater(object="" salary)="" salary);="" salary;="" static="" string="" string.format(name="" this.name="name;" this.salary="salary;" tostring()="" {="" {0:c}",="" }=""> empLhs.salary) ? true : false;
2}
3}
4
5class BubbleSorter
6{
7static public void Sort(object [] sortArray, CompareOp gtMethod)
8{
9for (int i=0 ; i<sortarray.length ",="" 'blake's="" (decimal)1000000.38),="" (emprhs.salary="" (gtmethod(sortarray[j],="" (int="" +="" 10000),="" 20000),="" 23000),="" 25000),="" 50000)};="" 7'",="" :="" ;="" []="" bool="" bubblesorter.sort(employees,="" class="" compareop="" compareop(employee.rhsisgreater);="" compareop(object="" console.readline();="" console.writeline(employees[i].tostring());="" decimal="" delegate="" emplhs="(Employee)" employee="" employee("arabel="" employee("avon="" employee("bill="" employee("karli="" employee("mortimer",="" employee("simon="" employee(string="" employeecompareop="new" employeecompareop);="" employees="{" emprhs="(Employee)" for="" from="" gates",="" i="0" i++)="" i<employees.length="" if="" j="i+1" j++)="" j<sortarray.length="" jones",="" lhs,="" lhs;="" main()="" mainentrypoint="" name,="" name;="" namespace="" new="" object="" override="" private="" public="" return="" rhs)="" rhs);="" rhs;="" rhsisgreater(object="" robinson",="" salary)="" salary);="" salary;="" sortarray[i]="sortArray[j];" sortarray[i]))="" sortarray[j]="temp;" static="" string="" string.format(name="" system;="" temp="sortArray[i];" this.name="name;" this.salary="salary;" tostring()="" using="" void="" watson",="" wrox.profcsharp.advancedcsharp="" {="" {0:c}",="" }=""> empLhs.salary) ? true : false;
10}
11}
12
13class BubbleSorter
14{
15static public void Sort(object [] sortArray, CompareOp gtMethod)
16{
17for (int i=0 ; i<sortarray.length (emprhs.salary="" (gtmethod(sortarray[j],="" (int="" ;="" bool="" compareop(employee.rhsisgreater)这里就象定义了一个监听装置,一旦发生了compareop(object="" compareop(object="" emplhs="(Employee)" employee="" employeecompareop="new" emprhs="(Employee)" for="" i++)="" if="" j="i+1" j++)="" j<sortarray.length="" lhs,="" lhs;="" object="" public="" return="" rhs)="" rhs)委托,再说说委托:="" rhs)这个事件就去执行employee.rhsisgreater的代码。="" rhs;="" rhsisgreater(object="" sortarray[i]="sortArray[j];" sortarray[i]))="" sortarray[j]="temp;" static="" temp="sortArray[i];" {="" }="" 代码中,首先声明了一个delegate="" 委托机制是促使事件发送与事件接受的一种对接策略,对象对周围信号的反应或在一定环境中所具备的对其它对象的通知行为的响应则被描述成所谓的“事件”,这可以类比人对周围世界反馈产生信号的能力。委托就是一种定向信号流:指定产生、接受信号者并产生信号反馈的技术。那么这段委托是怎么把程序连动起来的呢,看后面的代码。="" 接下来我们就去看看employee.rhsisgreater里面的东西。="" 首先是compareop=""> empLhs.salary) ? true : false;
18}
19
20public static bool RhsIsGreater(object lhs, object rhs)
21{
22Employee empLhs = (Employee) lhs;
23Employee empRhs = (Employee) rhs;
24return (empRhs.salary > empLhs.salary) ? true : false;
25}
26
27
28也就是说RhsIsGreater的参数是匹配CompareOp存在的,参数中没有直接使用Employee这个type而是采用了一种通用的做法,全都弄成object需要的时候再做转换,暂时还无法理解其深远的意义,先记着了。估计是定义委托时不能用这些自定义的type吧。
29那么这段代码是什么时候运行的呢,注意看这段
30
31****
32
33
34static public void Sort(object [] sortArray, CompareOp gtMethod)
35{
36for (int i=0 ; i<sortArray.Length ; i++)
37{
38for (int j=i+1 ; j<sortArray.Length ; j++)
39{
40if (gtMethod(sortArray[j], sortArray[i]))
41{
42object temp = sortArray[i];
43sortArray[i] = sortArray[j];
44sortArray[j] = temp;
45}
46}
47}
48}
49
50
51其中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)这样就执行到里面的代码了。
52整个事件响应完成。</sortarray.length></sortarray.length></employees.length>