ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 委托、事件--委托实例篇

委托、事件--委托实例篇

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-07-06 15:23:25 0 删除 编辑

实例一、

  1. using System;
  2. namespace SimpleDelegate
  3. {
  4.     //定义委托
  5.     delegate double DoubleOp(double x);
  6.  
  7.     class MainEntryPoint
  8.     {
  9.         static void Main()
  10.         {
  11.             //实例化
  12.             DoubleOp[] perations =
  13.                 {
  14.                     new DoubleOp(MathsOperations.MultiplyByTwo),
  15.                     new DoubleOp(MathsOperations.Square)
  16.                 };
  17.             /* 使用匿名方法
  18.             DoubleOp first = delegate(double value){return value * 2;};
  19.             DoubleOp second = delegate(double value) { return value * value; };
  20.             DoubleOp[] perations = { first, second };
  21.             */
  22.  
  23.             //使用委托
  24.             for (int i = 0; i < operations.Length; i++)
  25.             {
  26.                 Console.WriteLine("Using operations[{0}]:", i);
  27.                 ProcessAndDisplayNumber(operations[i], 2.0);
  28.                 ProcessAndDisplayNumber(operations[i], 7.94);
  29.                 ProcessAndDisplayNumber(operations[i], 1.414);
  30.                 Console.WriteLine();
  31.             }
  32.             Console.ReadLine();
  33.         }
  34.  
  35.         static void ProcessAndDisplayNumber(DoubleOp action, double value)
  36.         {
  37.             double result = action(value);
  38.             Console.WriteLine("Value is {0}, result of operation is {1}", value, result);
  39.         }
  40.     }
  41.  
  42.     class MathsOperations
  43.     {
  44.         public static double MultiplyByTwo(double value)
  45.         {
  46.             return value * 2;
  47.         }
  48.  
  49.         public static double Square(double value)
  50.         {
  51.             return value * value;
  52.         }
  53.    }
  54. }

      这段代码的关键一行是把委托传递给ProcessAndDisplayNumber()方法,例如:ProcessAndDisplayNumber(operations[i], 2.0);

      其中传递了委托名,但不带任何参数,假定operations[i]是一个委托,其语法是:

      1、operations[i]表示“这个委托”。换言之,就是委托代表的方法。

      2、operations[i](2.0)表示“调用这个方法,参数放在括号中”。

实例二、

  1. using System;
  2.  
  3. namespace BubbleSorter
  4. {
  5.     //定义委托
  6.     delegate bool CompareOp(object lhs, object rhs);
  7.  
  8.     class MainEntryPoint
  9.     {
  10.         static void Main()
  11.         {
  12.             Employee[] employees =
  13.             {
  14.                 new Employee("Bugs Bunny", 20000),
  15.                 new Employee("Elmer Fudd", 10000),
  16.                 new Employee("Daffy Duck", 25000),
  17.                 new Employee("Wiley Coyote", (decimal)1000000.38),
  18.                 new Employee("Foghorn Leghorn", 23000),
  19.                 new Employee("RoadRunner'", 50000)};
  20.             //实例化
  21.             CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater);
  22.             BubbleSorter.Sort(employees, employeeCompareOp);
  23.  
  24.             for (int i = 0; i < employees.Length; i++)
  25.                 Console.WriteLine(employees[i].ToString());
  26.             Console.ReadLine();
  27.         }
  28.     }
  29.  
  30.     class Employee
  31.     {
  32.         private string name;
  33.         private decimal salary;
  34.  
  35.         public Employee(string name, decimal salary)
  36.         {
  37.             this.name = name;
  38.             this.salary = salary;
  39.         }
  40.  
  41.         public override string ToString()
  42.         {
  43.             return string.Format(name + ", {0:C}", salary);
  44.         }
  45.  
  46.         //委托调用的方法
  47.         public static bool RhsIsGreater(object lhs, object rhs)
  48.         {
  49.             Employee empLhs = (Employee)lhs;
  50.             Employee empRhs = (Employee)rhs;
  51.             return (empRhs.salary > empLhs.salary) ? true : false;
  52.         }
  53.     }
  54.  
  55.     class BubbleSorter
  56.     {
  57.         static public void Sort(object[] sortArray, CompareOp gtMethod)
  58.         {
  59.             for (int i = 0; i < sortArray.Length; i++)
  60.             {
  61.                 for (int j = i + 1; j < sortArray.Length; j++)
  62.                 {
  63.                     //使用委托中的方法比较
  64.                     if (gtMethod(sortArray[j], sortArray[i]))
  65.                     {
  66.                         object temp = sortArray[i];
  67.                         sortArray[i] = sortArray[j];
  68.                         sortArray[j] = temp;
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-608416/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5298041