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> 静态绑定的更多相关文章

  1. C#_delegate - Pair<T> & 简单顺序逆序 & 方法委托(在Pair类下)&枚举类型 混搭使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. c++ pair 使用

    1. 包含头文件: #include <utility> 2. pair 的操作: pair<T1,T2> p; pair<T1,T2> p(v1,v2); pai ...

  3. 论Pair的重要性

    这些天我在用React和D3做图表,从已经实现的图表里复制了一些坐标轴的代码,发现坐标轴上的n个点里,只有第一个点下面能渲染出文字提示,其余点下面都无法渲染出文字. 和组里的FL一起百思不得其解好几天 ...

  4. PHP延迟静态绑定

    类可以自下往上调用父类方法,如果需要在父类中根据不同的子类,来调用子类的方法,那么就需要延迟静态绑定.延迟静态绑定用的是保留关键词static. 所谓延迟静态绑定,顾名思义,静态调用时::符号左侧的部 ...

  5. 2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair dfs序+分块

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  6. pair的使用

    #include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...

  7. 【C++】pair

    STL的pair,有两个值,可以是不同的类型. template <class T1, class T2> struct pair; 注意,pair在头文件utility中,不要inclu ...

  8. PHP "延迟静态绑定" 功能,static

    从这个名字的定义提取出两个关键点,第一点静态,也就是说这个功能只适用于静态属性或静态方法.第二点延迟绑定,这个根据下面代码就可以很好的理解 看一下这个例子: class A{ static $name ...

  9. hackerrank Similar Pair

    传送门 Problem Statement You are given a tree where each node is labeled from 1 to n. How many similar ...

随机推荐

  1. andori 动画验证必填项

    android项目开发过程中,都会碰到必填项的校验,最简单的就是利用Toast对用进行提示,感觉这种提示太不够人性化了,那么今天就来个带动画的,并可以将光标定位到必填项中. andorid动画Anim ...

  2. 在SharePoint 2010中创建网站的权限级别

    转:http://www.360sps.com/Item/CreatePermissionLevels.aspx 权限级别是SharePoint 2010新增加的功能,使我们对权限的设置又提高了一个层 ...

  3. Spring 拦截器配置

    Spring interceptor拦截器配置 Spring mvc的拦截器是通过handlerinterceptor来实现的 实现方式: 1.自定义一个类实现Spring的handlerinterc ...

  4. Mac 中查看端口占用进程并杀死

    sudo lsof -i :9000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 61342 a 313u IPv6 0x11111 ...

  5. HDU2015校赛 The Country List

    今天手感真差..各种读错题意.水题... 就是说,给你几个串.如果长度一样并且相同位置字符相同(不分大小写)的个数大于两个就是不同串. #include<iostream> #includ ...

  6. Mysql的函数使用方法

    今天有点临时需求要计算一张表的结果,不想写代码,想到了mysql的自定义函数.碰到了很多问题,为了方便一下使用,在此记录一下. 需求:一张表中,有比分,需要查询出比赛id和比赛结果. 分析:     ...

  7. WebAPI 小知识

    1.HttpResponseMessage.ReasonPhrase可以返回原因说明短语, 用JQuery中的$.ajax调用,返回函数第三个参数可以获取,如下: success:function(d ...

  8. 函数 stat() 详解

    先看看MSDN的解釋: stat(): Get status information on a file. Parameters:     path:  pointer to a string con ...

  9. poj 3250 Bad Hair Day【栈】

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15922   Accepted: 5374 Des ...

  10. .NET通用基本权限系统

    DEMO下载地址: http://download.csdn.net/detail/shecixiong/5372895 一.开发技术:B/S(.NET C# ) 1.Windows XP以上 (支援 ...