zoukankan      html  css  js  c++  java
  • C#委托冒泡

    委托的实现,就是编译器自行定义了一个类:有三个重要参数1.制定操作对象,2.指定委托方法3.委托链

    看如下一个列子:

     class DelegatePratice
        {
            public static void Sort<T>(IList<T> sortArray, Func<T, T, bool> Comparison)
            {
                bool swapped = true;
                do
                {
                    swapped= false;
                    for (int i = 0; i < sortArray.Count - 1; i++)
                    {
                        if (Comparison(sortArray[i], sortArray[i + 1]))
                        {
                            T temp = sortArray[i];
                            sortArray[i] = sortArray[i + 1];
                            sortArray[i + 1] = temp;
                            swapped = true;
                        }
                    }
                } while (swapped);
               
            }
        }

    定义实体类:
    class Employee
        {
            public Employee(string name, decimal salary)
            {
                this.Name = name;
                this.Salary = salary;
            }
            public string Name
            {
                get;
                set;
            }
            public decimal Salary
            {
                get;
                set;
            }
            /// <summary>
            /// 重写object的ToString方法
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                return string.Format("{0},{1:C}",Name,Salary);
            }
            
            /// <summary>
            /// 比较两员工的工资以此排序
            /// </summary>
            /// <param name="e1"></param>
            /// <param name="e2"></param>
            /// <returns></returns>
            public static bool Comparison(Employee e1, Employee e2)
            {
                return e1.Salary > e2.Salary;
            }
        }

     调用

     class Program
        {
            static void Main(string[] args)
            {
                Employee[] employees = { new Employee("simen", 5500), new Employee("shelly", 5900),
                    new Employee("pool", 4300), new Employee("renata", 3800) };
                DelegatePratice.Sort(employees, Employee.Comparison);
                foreach (Employee employee in employees)
                {
                    Console.WriteLine(employee);
                }
                Console.ReadKey();
            }
        }
    }
    

      

      

  • 相关阅读:
    GUI 监听事件 (两个按钮,实现同一个监听)
    GUI 监听事件
    GUI 练习
    GUI 之表格布局
    GUI 之边界布局
    GUI 之流布局
    [转帖]Linux 下解压 rar 文件
    Linux 启动、停止、重启jar包脚本
    关于linux下,ls vi等命令失效的解决方法(配置下环境变量出现问题)
    超好用的UnixLinux 命令技巧 大神为你详细解读
  • 原文地址:https://www.cnblogs.com/simen-tan/p/5406526.html
Copyright © 2011-2022 走看看