博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#_delegate - Pair<T> 静态绑定
阅读量:6266 次
发布时间:2019-06-22

本文共 3186 字,大约阅读时间需要 10 分钟。

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
{ //存放两个对象的私有数组 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
.WhichIsFirst OderDog = new Pair
.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
.WhichIsFirst OrderStudents = new Pair
.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
studentPair = new Pair
(Mary,Jess); Pair
dogPair = new Pair
(Milo, Free); studentPair.Sort(Student.OrderStudents) ; Console.WriteLine(studentPair.ToString()); Console.ReadLine(); } }}

转载于:https://www.cnblogs.com/MarchThree/p/3720446.html

你可能感兴趣的文章
Mysql索引会失效的几种情况分析
查看>>
LVM逻辑卷
查看>>
zoj3591 Nim(Nim博弈)
查看>>
canvas绘图
查看>>
poj - 3039 Margaritas on the River Walk
查看>>
bootstrap(5)关于导航
查看>>
Aptana插件在eclipse中安装
查看>>
jQuery-数据管理-删除事件
查看>>
下载器简单实例
查看>>
java实现分页工具类(JDBC)
查看>>
欧几里德算法与扩展欧几里德算法
查看>>
Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2)
查看>>
通过kafka提供的命令来查看offset消费情况
查看>>
oracle数据库从入门到精通之四
查看>>
自定义圆形图片控件
查看>>
sharepoint 2013 补丁升级步骤
查看>>
asp.net core 2.0 web api基于JWT自定义策略授权
查看>>
Skype for Business Server 2015-04-前端服务器-3-安装-管理工具
查看>>
第12章代码《跟老男孩学习Linux运维:Shell编程实战》
查看>>
我们为什么从Python转到go?
查看>>