首页 > 开发 > 综合 > 正文

读书笔记c#高级编程 委托和事件

2024-07-21 02:17:34
字体:
来源:转载
供稿:网友
 

在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;
}

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




发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表