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. Android 主题动态切换框架:Prism

    Prism(棱镜) 是一个全新的 Android 动态主题切换框架,虽然是头一次发布,但它所具备的基础功能已经足够强大了!本文介绍了 Prism 的各种用法,希望对你会有所帮助,你也可以对它进行扩展, ...

  2. 利用文件实现Free Pascal中的简单排序功能

    此程序主要是验证文件功能的读写功能,总结到的东西有:①文件无论是读还是写,都要先建立链接关系才可以进行;②读与写不能同时进行,必须分开操作,这也可以理解,在实际鼠标操作时也是如此的!③读写后必须用cl ...

  3. [JDBC-1] JDBC Base Template

    以Statement建立的标准模板: static void template() throws Exception { Connection conn = null; Statement st = ...

  4. msp时钟设置程序

    吐槽一下MSP430需要明白的东西: 在430中,一个时钟周期 = MCLK晶振的倒数.如果MCLK是8M,则一个时钟周期为1/8us: 一个机器周期 = 一个时钟周期,即430每个动作都能完成一个基 ...

  5. SSDT Hook结构

    目录 SSDT Hook效果图 SSDT简介 SSDT结构 SSDT HOOK原理 Hook前准备 如何获得SSDT中函数的地址呢 SSDT Hook流程 SSDT Hook实现进程保护 Ring3与 ...

  6. Leetcode OJ : Restore IP Addresses backtrack暴搜 C++ solution

    class Solution { public: vector<string> ret; string src; int len; unordered_set<string> ...

  7. uva 2572 Viva Confetti

    思路: 小圆面是由小圆弧围成.那么找出每条小圆弧,如果小圆弧,在小圆弧中点上下左右进行微小位移的所得的点一定在一个小圆面内. 找到最后覆盖这个小点的圆一定是可见的. 圆上的点按照相邻依次排序的关键量为 ...

  8. Linode各机房在中国访问速度性能测试

    最近因为google的各种被X的原因,想自己弄个VPS玩玩,比来比去都推荐linode. 因为各种性能测试工具都不靠谱,还是自己机器来的直接,虽然笨拙但是真实可信. 从测试结果上看,明显东京机房的速度 ...

  9. jQuery遍历多层json数据

    1.json与jsonp的区别(待查) 2.要遍历的数据如下: {"status": "ok", "code": 200, "da ...

  10. web开发工具类

    1.日期工具类 import java.text.SimpleDateFormat; import java.util.Date; public class DateUtil { public sta ...