C#_delegate - Pair<T> 静态绑定
2013-09-19 13:18
204 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Starter { public enum Comparison { theFirstComesFirst = 1, theSecondComesFirst = 2 } //包含两项的简单集合 public class Pair<T> { //存放两个对象的私有数组 private T[] thePair = new T[2]; //委托声明 public delegate Comparison WhichIsFirst(T obj1, T obj2); //构造方法,参数为两个对象,按接收顺序添加 public Pair(T firstObject, T secondObject) { thePair[0] = firstObject; thePair[1] = secondObject; } //公共方法,按对象具体顺序范畴排序 - 被绑定的函数的返回值作为函数的参数 public void Sort(WhichIsFirst theDelegatedFunc) { if (theDelegatedFunc(thePair[0], thePair[1]) == Comparison.theSecondComesFirst) { T temp = thePair[0]; thePair[0] = thePair[1]; thePair[1] = temp; } } //反序排列 public void ReverSort(WhichIsFirst theDelegatedFunc) { if (theDelegatedFunc(thePair[0], thePair[1]) == Comparison.theFirstComesFirst) { T temp = thePair[0]; thePair[0] = thePair[1]; thePair[1] = temp; } } //要求两个对象提供字符串值 public override string ToString() { return thePair[0].ToString() + ", " + thePair[1].ToString(); } } public class Dog { //静态绑定 public static readonly Pair<Dog>.WhichIsFirst OderDog = new Pair<Dog>.WhichIsFirst(WhichDogComesFirst); private int weight; public Dog(int weight) { this.weight = weight; } public static Comparison WhichDogComesFirst(Dog d1,Dog d2) { return d1.weight > d2.weight ? Comparison.theSecondComesFirst : Comparison.theFirstComesFirst; } public override string ToString() { return weight.ToString(); } } public class Student { //类中进行静态绑定 public static readonly Pair<Student>.WhichIsFirst OrderStudents = new Pair<Student>.WhichIsFirst(Student.WhichStudentComesFirst); private string name; public Student(string name) { this.name = name; } public static Comparison WhichStudentComesFirst(Student s1, Student s2) { return (String.Compare(s1.name, s2.name)<0 ? Comparison.theFirstComesFirst : Comparison.theSecondComesFirst ); } public override string ToString() { return name; } } class Program { static void Main(string[] args) { Student Jess = new Student("Jess"); Student Mary = new Student("Mary"); Dog Milo = new Dog(26); Dog Free = new Dog(12); Pair<Student> studentPair = new Pair<Student>(Mary,Jess); Pair<Dog> dogPair = new Pair<Dog>(Milo, Free); studentPair.Sort(Student.OrderStudents) ; Console.WriteLine(studentPair.ToString()); Console.ReadLine(); } } }
相关文章推荐
- C#_delegate - Pair<T> & 简单顺序逆序 & 方法委托(在Pair类下)&枚举类型 混搭使用
- KeyValuePair<byte, string> 类型的数据如何绑定到ComboBox控件上
- C#中使用BindingList 绑定DataGridView 可以实现动态绑定,优与List<T>集合的绑定
- C# KeyValuePair<TKey,TValue>的用法-转载
- C# Dictionary的用法 KeyValuePair<TKey, TValue> 结构
- C# delegate Action<T> lambda表达式
- C# KeyValuePair<TKey,TValue>的用法【转】
- C#页面前台绑定数据 <%%><%#%><%=%>
- C# Winform DataGrid 绑定List<> Or ObservableCollection<> 类型无法自动刷新问题
- C# KeyValuePair<TKey,TValue>的用法【转】
- C#中的delegate以及利用List<T>中delegate快速实现排序、查找
- C#基础第四天-作业-Hashtable-list<KeyValuePair>泛型实现名片
- C#基础第四天-作业答案-Hashtable-list<KeyValuePair>泛型实现名片
- C# KeyValuePair<TKey,TValue>的用法
- <win8>(二)实例讲解win8(XAML+C#)开发--------课程表:数据绑定
- 用c# 实现设计器 DesignSurface<转>
- C# List<T>转为Json数据
- 黑马程序员——>第十七天<集合(Collections-增强for循环-可变参数-静态导入)>
- C# 中的INotifyPropertyChanged和ObservableCollection<T>
- List<T>做数据源绑定的问题