委托使用(1)

这是一个委托排序的例子

public enum Comparsion    

{        

theFirstComesFirst=1,        

theSecondComesFirst=2    

}    //来决定排序的顺序

class Pair<T>    //在下面我创建的两个类Student,Dog,都把他们保存在Pair里面

{        

private T[] thePair = new T[2];        

public  delegate Comparsion WhichIsFirst(T obj1,T obj2); //定义委托       

public Pair(T firstObject,T secondObject)        

{           

 thePair[0] = firstObject;            

thePair[1] = secondObject;        

 /// <summary>        

/// 排序的过程        

/// </summary>        

/// <param name="whileIsFirst">一个委托实例的传入</param>               

public void Sort(WhichIsFirst whileIsFirst)        

{                        

if (whileIsFirst(thePair[0], thePair[1]) == Comparsion.theSecondComesFirst)            

{                

T same;                

same = thePair[0];                

thePair[0] = thePair[1];                

thePair[1] = same;            

}        

public override string ToString()  //重载ToString()方法      

{            

return thePair[0].ToString()+thePair[1].ToString(); //调用Studnt或Dog中的ToString()方法       

}    

}

定义Student类

 class Student    {        

public static readonly Pair<Student>.WhichIsFirst w = new Pair<Student>.WhichIsFirst(Student.StudentWhileIsFirst);//如果不想再客户端中申明委托实例,可是在类中写一个静态只读的委托实例        

private String _name;        

public Student(String name)        

{            

this._name = name;        

}        

public static Comparsion StudentWhileIsFirst(Student s1,Student s2)//这里我用的是静态方法,定义一个委托的方法,要跟定义委托的格式一样        

{            

return String.Compare(s1._name, s2._name) < 0 ? Comparsion.theFirstComesFirst : Comparsion.theSecondComesFirst;        

}        

public override string ToString()        

{            

return _name;        

}    

}

定义Dog类

class Dog    

{        

private String _weight;        

public Dog(String weight)        

{            

this._weight = weight;        

}        

public  Comparsion DogWhileIsFirst(Dog d1, Dog d2) //这里我用的是类的实例方法       

{            

return String.Compare(d1._weight, d2._weight) < 0 ? Comparsion.theFirstComesFirst : Comparsion.theSecondComesFirst;        

}        

public override string ToString()       

{            

return _weight;        

}    

}

客户端代码

Student s1=new Student("gu");            

Student s2=new Student("bao");            

Pair<Student> pairStudent = new Pair<Student>(s1, s2);            

//Pair<Student>.WhichIsFirst stuWhile=new Pair<Student>.WhichIsFirst(Student.StudentWhileIsFirst); //如果Student中没有那个静态只读的成员,就用这一句在然后再调用  pairStudent.Sort(stuWhile)        

pairStudent.Sort(Student.w);            

Console.WriteLine("pairStudent:{0}",pairStudent.ToString());            

Dog d1 = new Dog("56");            

Dog d2 = new Dog("54");            

Pair<Dog> pairDog = new Pair<Dog>(d1,d2);            

Pair<Dog>.WhichIsFirst dogWhile = new Pair<Dog>.WhichIsFirst(d1.DogWhileIsFirst);            

pairDog.Sort(dogWhile);            

Console.WriteLine("dogStudent:{0}", pairDog.ToString());

原文地址:https://www.cnblogs.com/gull/p/1857221.html