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();
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值