委托示例(利用委托对不同类型的对象数组排序)

来源:互联网 发布:淘宝设计网 编辑:程序博客网 时间:2024/05/16 08:53
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace delegateTest
  6{
  7    /// <summary>
  8    /// 演示利用委托给不同类型的对象排序
  9    /// </summary>

 10    class Program
 11    {
 12        delegate bool CompareOp(object lhs,object rhs);//声明委托(注意方法签名的格式是两个object类型参数)
 13
 14        static void Main(string[] args)
 15        {
 16            Employee[] employees = {
 17                new Employee("Bugs Bunny",20000),
 18                new Employee("Elmer Fudd",10000),
 19                new Employee("Daffy Duck",25000),
 20                new Employee("Wiley Coyote",(decimal)1000000.38),
 21                new Employee("Foghorn Leghorn",23000),
 22                new Employee("Road Runner",50000)
 23            }
;
 24
 25            CompareOp c1 = new CompareOp(Employee.CompareEmploySalary);
 26
 27            BubbleSorter.Sort(employees, c1);//对employees数组,按工资高低排序
 28
 29            for (int i = 0; i < employees.Length; i++)
 30            {
 31                Console.WriteLine(employees[i].ToString());
 32            }

 33
 34            Console.WriteLine("---------------------------------------");
 35
 36            object[] ints = 352860 };
 37
 38            c1 = new CompareOp(CompareInt);
 39            BubbleSorter.Sort(ints, c1);//对ints数组,按数值大小排序
 40            for (int i = 0; i < ints.Length; i++)
 41            {
 42                Console.WriteLine(ints[i].ToString());
 43            }

 44
 45            Console.WriteLine("---------------------------------------");           
 46
 47            Console.ReadLine();
 48        }

 49
 50        /// <summary>
 51        /// 比较整数的大小
 52        /// </summary>
 53        /// <param name="x">整数1</param>
 54        /// <param name="y">整数2</param>
 55        /// <returns>如果第一个数小于第二数,返回true,反之false</returns>

 56        static bool CompareInt(object x, object y) 
 57        
 58            return (int)y>(int)x?true:false;
 59        }

 60
 61        /// <summary>
 62        /// 冒泡排序类
 63        /// </summary>

 64        class BubbleSorter
 65        {
 66            static public void Sort(object[] sortArray, CompareOp gtMethod)
 67            {
 68                for (int i = 0; i < sortArray.Length; i++)
 69                {
 70                    for (int j = i + 1; j < sortArray.Length; j++)
 71                    {
 72                        if (gtMethod(sortArray[j], sortArray[i])) //比较大小,注:不同的object,比较大小的方法不同,比如Employee是按工资高低来比较,int是按数字大小来比较,利用委托的好处就在于不用管具体用哪种方法,具体调用的时候才确定用哪种方法
 73                        {
 74                            object temp = sortArray[i];
 75                            sortArray[i] = sortArray[j];
 76                            sortArray[j] = temp;
 77                        }

 78                    }

 79
 80                }

 81            }

 82        }

 83    }

 84
 85    
 86    /// <summary>
 87    /// 员工实体类
 88    /// </summary>

 89    class Employee 
 90    {
 91        private string name;
 92        private decimal salary;
 93
 94
 95        /// <summary>
 96        /// Employee构造函数
 97        /// </summary>
 98        /// <param name="name"></param>
 99        /// <param name="salary"></param>

100        public Employee(string name, decimal salary) 
101        {
102            this.name = name;
103            this.salary = salary;
104        }

105
106        /// <summary>
107        /// 覆盖ToString()方法
108        /// </summary>
109        /// <returns></returns>

110        public override string ToString()
111        {
112            return string.Format(name + ",{0:c}", salary);
113        }

114
115        /// <summary>
116        /// 按员工工资高低比较大小
117        /// </summary>
118        /// <param name="lhs"></param>
119        /// <param name="rhs"></param>
120        /// <returns></returns>

121        public static bool CompareEmploySalary(object lhs, object rhs) 
122        {
123            Employee empLhs = (Employee)lhs;
124            Employee empRhs = (Employee)rhs;
125            return (empRhs.salary > empLhs.salary) ? true : false;
126        }

127    }

128}

129
运行结果:
Elmer Fudd,¥10,000.00
Bugs Bunny,¥20,000.00
Foghorn Leghorn,¥23,000.00
Daffy Duck,¥25,000.00
Road Runner,¥50,000.00
Wiley Coyote,¥1,000,000.38
---------------------------------------
0
2
3
5
6
8
---------------------------------------
 
原创粉丝点击