您的位置:程序门 -> .net技术 -> c#



委托的解释?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


委托的解释?[已结贴,结贴人:zhangchen124]
发表于:2007-09-06 12:33:31 楼主
委托是类还是其他?
发表于:2007-09-06 12:37:441楼 得分:3
指向函数的指针
发表于:2007-09-06 12:37:452楼 得分:3
我的理解:委托是函数地址指针,你用ildsm看一下il代码会发现,是一个类
发表于:2007-09-06 13:44:293楼 得分:3
http://blog.csdn.net/acewang/archive/2004/08/02/58394.aspx
发表于:2007-09-06 14:36:224楼 得分:3
编译器看到你用delegate定义一个新的委托时,它就会在背后生成许多代码,而且,你这个委托类型是变成了一个类型
发表于:2007-09-06 15:32:385楼 得分:3
委托是一种引用数据类型,它的实例能够封装与其签名相匹配的类的静态方法引用或类的实例方法引用

委托类似于c   或   c++   中的函数指针。然而,函数指针只能引用静态函数,而委托可以引用静态方法和实例方法

所有委托都是从system.delegate继承而来的,并且有一个调用列表,这是在调用委托时所执行的方法的一个链接列表。
发表于:2007-09-06 16:09:116楼 得分:2
http://blog.joycode.com/percyboy/archive/2005/01/22/43435.aspx
事件模型可以参考一下
发表于:2007-09-06 16:16:457楼 得分:2
這個問題,如果你要把委撬說成是類,本也沒有錯,但你要做好挨罵的心里准備,說她是接口也好像沒有錯。呵呵.....
技朮這東西..
发表于:2007-09-06 17:19:228楼 得分:1
c#委托事件  
在c#中使用一个类时,分两个阶段.首先需要定义这个类,即告诉编译器这个类由什么字段和方法组成.然后(除非只使用静态方法)实例化类的一个对象.使用委托时,也需要经过这两个步骤.首先定义要使用的委托,对于委托,定义它就是告诉编译器这种类型代表了那种类型的方法,然后创建该委托的一个或多个实例.定义委托是从delegate开始的然而它是如何运作的呢.也许弄个鼠标事件会容易理解一些,这里还是拿出书中的例子来.  
using   system;
namespace   wrox.profcsharp.advancedcsharp
{
        delegate   bool   compareop(   object   lhs,   object   rhs   );
        class   mainentrypo
        int
        {
                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   mainentrypo
        int
        {
                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;
}

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   )这样就执行到里面的代码了.整个事件响应完成.



快速检索

最新资讯
热门点击