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. Hibernate4.x之映射关系--双向1-n

    双向1-n与双向n-1是完全相同的两种情形 双向1-n需要在1的一端可以访问n的一端,反之亦然. 域模型:从Order到Customer的多对一双向关联需要在Order类中定义一个Customer属性 ...

  2. Sping表达式语言--SpEL

    Spring表达式语言---SpEL 是一个支持运行时查询和操作对象的强大的表达式语言 语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都将被认为是SpEL SpEL为bean ...

  3. [JDBC-1] JDBC Base Template

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

  4. .net 禁止远程查看应用程序错误的详细信息,服务器上出现应用程序错误

    打开页面时出现以下错误   "/"应用程序中的服务器错误. 运行时错误 说明: 服务器上出现应用程序错误.此应用程序的当前自定义错误设置禁止远程查看应用程序错误的详细信息(出于安全 ...

  5. win7(64位)+IE8+QC9.0

    环境win7(64位)+IE8+QC9.0出现的问题IE8访问QC9.0有时访问登录显示正常,但是有时访问QC页面无法显示正常,然后在ie8中安全中设置“启用内存保护帮助减少联机攻击*”也无法找到该项 ...

  6. Serv-U搭建FTP服务器

    1.打开软件,勾选start automatically 2.点击domain,新建domain 3.依次输入IP.端口号.域名.域名类型 完成后的样子 4.右键单击Users,新建用户.依次输入用户 ...

  7. Magento输入正确的登陆名和密码无法进入后台

    请找到 这个文件夹: app\code\core\Mage\Core\Model\Session\Abstract 然后修改Varien.php这个文件: 找到代码(大约88行): $cookiePa ...

  8. javascript活动对象的理解——伪单例模式

    在自己研究javascript各种设计模式的过程中,偶然写出的一段代码让自己理解的更深刻了,之所以称之为伪单例模式,是因为这段代码造成的结果很想单例模式,但是实际上是活动对象捣乱所造成的误会. 代码很 ...

  9. DataRow数组 转 datatable

    DataTable tmpdt = dt.Clone(); DataRow[] drs = dt.Select("legnbr="+legNbr); ) { tmpdt = drs ...

  10. linux内存负载分析

    衡量内存负载的一个很重要的指标就是页面置换的频率.当linux系统频繁的对页进行换进换出 的时候,说明物理内存不过,不得不进行频繁的置换页面. 使用vmstat(virtual memory stat ...