C#_delegate - Pair<T> & 简单顺序逆序 & 方法委托(在Pair类下)&枚举类型 混搭使用
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类下)&枚举类型 混搭使用的更多相关文章
- 【C语言】输入5个整数并按输入顺序逆序输出
#include <stdio.h> int main() { ],i; printf("请输入5个整数:\n"); ;i<;i++) scanf("% ...
- php 简单计算权重的方法(适合抽奖类的应用)
//简单权重计算器 $data222=array( 0=>array('id'=>1,'name'=>'一等奖','weight'=>'3'), 1=>a ...
- [六] 函数式接口的复合方法示例 predicate 谓词逻辑运算 Function接口 组合运算 比较器 逆序 比较链
复合的方法 有些函数式接口提供了允许复合的方法 也就是可以将Lambda表达式复合成为一个更加复杂的方法 之前的章节中有说到: 接口中的compose, andThen, and, or, negat ...
- 链表逆序(JAVA实现)
题目:将一个有链表头的单向单链表逆序 分析: 链表为空或只有一个元素直接返回: 设置两个前后相邻的指针p,q,使得p指向的节点为q指向的节点的后继: 重复步骤2,直到q为空: 调整链表头和链表尾: 图 ...
- shell技巧之以逆序形式打印行
测试文本内容如下: # cat textfile hadoop hdfs yarn spark zookeeper mapreduce hive hbase scala kafka CHAVIN my ...
- 实体类的枚举属性--原来支持枚举类型这么简单,没有EF5.0也可以
通常,我们都是在业务层和界面层使用枚举类型,这能够为我们编程带来便利,但在数据访问层,不使用枚举类型,因为很多数据库都不支持,比如我们现在用的SqlServer2008就不支持枚举类型的列,用的时候也 ...
- 简单工厂VS工厂方法
前言: GOF经典的23种设计模式在IT界现已被广为流传.由于比较长时间没有用了,个人对于不同模式与模式之间的区别也渐渐模糊,故开始重温设计模式的思想.也希望更给对设计模式感兴趣的朋友些许的启发. - ...
- MFC中窗口启动后,CEdit编辑框默认光标位置设置,顺序的调节方法
MFC中窗口启动后,CEdit编辑框默认光标位设置,顺序的调节方法 在编辑界面按下ctrl+D键,就会出现所有控件的Tab键顺序,按照自己想要的顺序依次点击控件,就可以重新安排顺序.数值1就是默认停留 ...
- C# 正则表达式中的顺序环视和逆序环视
环视结构不匹配任何字符,只匹配文本中的特定位置. 顺序环视:从左向右查看文本,尝试匹配子表达式,如果能够匹配则返回匹配成功信息.顺序环视使用「 (?=...) 来标识」,例如「 (?=\d) 」,它表 ...
随机推荐
- MySQL 存储过程删除大表
1.权限问题 alter routine 编辑或删除存储过程 create routine 建立存储过程 execute 创建存储过程 2.存储过程相关的一些命令 show procedure sta ...
- show slave status中的log_file / log_pos
在MySQL的master-slave或dual master的架构中,我们经常使用show slave status命令来查看复制状态. 这里涉及几个重要的日志文件和位置: Master_Log_F ...
- HDU 5639 Deletion 二分+网络流
题意:bc round 74 div1 分析: 考虑删掉的边的形态, 就是我们经常见到的环套树这种结构, 参考平时这种图给出的方法, 如果一个图的每个点的出边只有一条, 那么一定会构成环套树这种结构. ...
- selenium 处理iframe
这篇关于iframe的文章不错 http://assertselenium.com/2013/02/22/handling-iframes-using-webdriver/ selenium处理ifr ...
- opencv2.4.4 背景减除算法收集
算法集合:https://code.google.com/p/bgslibrary/ 测试:AdaptiveBackgroundLearning算法 #include <iostream> ...
- 初识SQL注入
什么是SQL注入(SQL Injection)? SQL注入是网站攻击途径之一,这里引用一下百度百科的解释:“所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串, ...
- 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇02:内购如何实现》
2.内购如何实现 内购概述: 游戏内购是指玩家在游戏中,用金钱获取游戏道具的一种方式.开发者从玩家内购之中获得开发游戏的收益,因此大量游戏都有内购模块.下面我们以在手机IOS平台实现跑酷游戏内购为例, ...
- 深度学习多机多卡解决方案-purine
未经允许请不要转载,原作者:zhxfl,http://www.cnblogs.com/zhxfl/p/5287644.html 目录: 一.简介 二.环境配置 三.运行demo 四.硬件配置建议 五. ...
- mapreduce学习指导及疑难解惑汇总
原文链接http://www.aboutyun.com/thread-7091-1-1.html 1.思想起源: 我们在学习mapreduce,首先我们从思想上来认识.其实任何的奇思妙想,抽象的,好的 ...
- Java持久化存储对象Properties的方法list、store、load
package FileDemo; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStrea ...