尔冬橙

博客园 首页 新随笔 联系 订阅 管理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            Dog d1 = new Dog(65);
            Dog d2 = new Dog(12);
            Pair dogPair = new Pair(d1, d2);
            Console.WriteLine("dogPair \t:{0}",dogPair.ToString());
            dogPair.sort(Dog.orderDogs);
            Console.WriteLine("After sort dogPair \t:{0}",dogPair.ToString());

            Console.ReadLine();
        }
    }
    public enum comparison
    { 
        theFirstComesFirst = 1,
        theSecordComesFirst = 2
    }
    public delegate comparison WhichIsFirst(object obj1, object obj2);
    public class Pair
    {
        private object[] thePair = new object[2];

        public Pair(object firstObject, object secondObject)
        {
            thePair[0] = firstObject;
            thePair[1] = secondObject;
            
        }
        public void sort(WhichIsFirst theDelegatedFunc)
        { 
            if(theDelegatedFunc(thePair[0],thePair[1])==comparison.theSecordComesFirst)
            {
                object 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 static WhichIsFirst orderDogs
        {
            get
            {
                return new WhichIsFirst(WhichDogComesFirst);
            }
        }
        public Dog(int weight)
        {
            this.weight = weight;
        }
        public static comparison WhichDogComesFirst(object obj1, object obj2)
        {
            Dog d1 = (Dog)obj1;
            Dog d2 = (Dog)obj2;
            return d1.weight > d2.weight ? comparison.theSecordComesFirst : comparison.theFirstComesFirst;
        }
        public override string ToString()
        {
            return weight.ToString();
        }
    }
}

  

posted on 2012-07-22 12:46  尔冬橙  阅读(134)  评论(0编辑  收藏  举报