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

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

posted @ 2008-12-16 17:59  SpringX  阅读(156)  评论(0编辑  收藏  举报