using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; //如果账户金额小于0 触发事件 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
{
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
{
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>(Jess,Mary);
Pair<Dog> dogPair = new Pair<Dog>(Milo, Free); Console.WriteLine("student pair: " + studentPair.ToString());
Console.WriteLine("dog pair: " + dogPair.ToString()); //Pair下的委托函数 new 出来,Student下的WhichStudentComesFirst绑定到委托函数,参数列表需要相同
Pair<Student>.WhichIsFirst theStudentDelegate = new Pair<Student>.WhichIsFirst(Student.WhichStudentComesFirst); Pair<Dog>.WhichIsFirst theDogDelegate = new Pair<Dog>.WhichIsFirst(Dog.WhichDogComesFirst); studentPair.Sort(theStudentDelegate);
Console.WriteLine("student after sort: " + studentPair.ToString());
studentPair.ReverSort(theStudentDelegate);
Console.WriteLine("student after ReverSort: " + studentPair.ToString());
Console.ReadLine();
}
} }

C#_delegate - Pair<T> & 简单顺序逆序 & 方法委托(在Pair类下)&枚举类型 混搭使用的更多相关文章

  1. 【C语言】输入5个整数并按输入顺序逆序输出

    #include <stdio.h> int main() { ],i; printf("请输入5个整数:\n"); ;i<;i++) scanf("% ...

  2. php 简单计算权重的方法(适合抽奖类的应用)

    //简单权重计算器 $data222=array(     0=>array('id'=>1,'name'=>'一等奖','weight'=>'3'),     1=>a ...

  3. [六] 函数式接口的复合方法示例 predicate 谓词逻辑运算 Function接口 组合运算 比较器 逆序 比较链

    复合的方法 有些函数式接口提供了允许复合的方法 也就是可以将Lambda表达式复合成为一个更加复杂的方法 之前的章节中有说到: 接口中的compose, andThen, and, or, negat ...

  4. 链表逆序(JAVA实现)

    题目:将一个有链表头的单向单链表逆序 分析: 链表为空或只有一个元素直接返回: 设置两个前后相邻的指针p,q,使得p指向的节点为q指向的节点的后继: 重复步骤2,直到q为空: 调整链表头和链表尾: 图 ...

  5. shell技巧之以逆序形式打印行

    测试文本内容如下: # cat textfile hadoop hdfs yarn spark zookeeper mapreduce hive hbase scala kafka CHAVIN my ...

  6. 实体类的枚举属性--原来支持枚举类型这么简单,没有EF5.0也可以

    通常,我们都是在业务层和界面层使用枚举类型,这能够为我们编程带来便利,但在数据访问层,不使用枚举类型,因为很多数据库都不支持,比如我们现在用的SqlServer2008就不支持枚举类型的列,用的时候也 ...

  7. 简单工厂VS工厂方法

    前言: GOF经典的23种设计模式在IT界现已被广为流传.由于比较长时间没有用了,个人对于不同模式与模式之间的区别也渐渐模糊,故开始重温设计模式的思想.也希望更给对设计模式感兴趣的朋友些许的启发. - ...

  8. MFC中窗口启动后,CEdit编辑框默认光标位置设置,顺序的调节方法

    MFC中窗口启动后,CEdit编辑框默认光标位设置,顺序的调节方法 在编辑界面按下ctrl+D键,就会出现所有控件的Tab键顺序,按照自己想要的顺序依次点击控件,就可以重新安排顺序.数值1就是默认停留 ...

  9. C# 正则表达式中的顺序环视和逆序环视

    环视结构不匹配任何字符,只匹配文本中的特定位置. 顺序环视:从左向右查看文本,尝试匹配子表达式,如果能够匹配则返回匹配成功信息.顺序环视使用「 (?=...) 来标识」,例如「 (?=\d) 」,它表 ...

随机推荐

  1. 关于设置sqlplus提示符样式的方法

    摘要:大家在日常工作中,我想99%都会用到sqlplus工具来登陆你的数据库,对数据库进行管理.调优.配置.运维.那么如果有n多台数据库的时候,我们在连接后全部是统一的SQL>提示符,就有可能发 ...

  2. PC端使用opencv获取webcam,通过socket把Mat图像传输到android手机端

    demo效果图: PC端 android端 大体流程 android端是服务器端,绑定IP和端口,监听来自PC端的连接, pc端通过socket与服务器andorid端传输图片. 主要代码 andro ...

  3. NGUI如何创建自己的精灵图集

    说实话其实很简单,但是在不知道的情况下真的不好弄啊. 1. 选择你要制作精灵图集的图片,可以选择多张 2. 提倡使用快捷键Alt + Shift + M 会有如下窗口弹出,也可以NGUI --> ...

  4. 【暑假】[实用数据结构]前缀树 Trie

    前缀树Trie Trie可理解为一个能够快速插入与查询的集合,无论是插入还是查询所需时间都为O(m) 模板如下: +; ; struct Trie{ int ch[maxnode][sigma_siz ...

  5. MVC Razor 语法(转)

    http://blog.csdn.net/pasic/article/details/7072340 原文地址:MVC Razor 语法(转)作者:panzhaowen_jacki 语法名称 Razo ...

  6. sql经典语句

    1.表形式如下:Year       Salary2000        10002001        20002002        30002003        4000想得到如下形式的查询结 ...

  7. HW6.16

    import java.util.Arrays; public class Solution { public static void main(String[] args) { int[] arra ...

  8. POJ1038 - Bugs Integrated, Inc.(状态压缩DP)

    题目大意 要求你在N*M大小的主板上嵌入2*3大小的芯片,不能够在损坏的格子放置,问最多能够嵌入多少块芯片? 题解 妈蛋,这道题折腾了好久,黑书上的讲解看了好几遍才稍微有点眉目(智商捉急),接着看了网 ...

  9. [C++]VS与第三方工具下载

    名称:Qt 5.1.1 (商业版与开放源码许可GPL/LGPL) 说明:Qt是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架 下载:http://www.qt.io/downl ...

  10. [Objective-c 基础 - 1.2] OC的基本类

    #import <Foundation/Foundation.h> typedef enum {GenderMan, GenderFemale} Gender; typedef enum ...