委託、事件--委託例項篇

iDotNetSpace發表於2009-07-06

例項一、

  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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章