.............using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace cs12_1
{
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 ReverseSort(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 Jesse = new Student("Jesse");
Student Stacey = new Student("Stacey");
Dog Milo = new Dog(65);
Dog Fred = new Dog(12);
Pair<Student> studentPair = new Pair<Student>(Jesse, Stacey);
Pair<Dog> dogPair = new Pair<Dog>(Milo, Fred);
Console.WriteLine("studentPair\t\t\t:{0}",
studentPair.ToString());
Console.WriteLine("dogPair\t\t\t\t:{0}",
dogPair.ToString()); 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("After Sort StudentPair\t\t:{0}",
studentPair.ToString());
studentPair.ReverseSort(theStudentDelegate); Console.WriteLine("After ReverseSort studentPair\t:{0}",
studentPair.ToString());
dogPair.Sort(theDogDelegate);
Console.WriteLine("After Sort dogPair\t\t:{0}",dogPair.ToString());
dogPair.ReverseSort(theDogDelegate);
Console.WriteLine("After ReverseSort dogPair\t:{0}",
dogPair.ToString());
Console.Read(); }
}
}
其中的: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 class Pair<T>,这个<T>是什么?集合吗?这里如何理解?
private T[],又如何理解?class Pair<T>类型的数组?
public delegate Comparison WhichIsFirst(T obj1, T obj2);这里的T又如何理解???
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace cs12_1
{
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 ReverseSort(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 Jesse = new Student("Jesse");
Student Stacey = new Student("Stacey");
Dog Milo = new Dog(65);
Dog Fred = new Dog(12);
Pair<Student> studentPair = new Pair<Student>(Jesse, Stacey);
Pair<Dog> dogPair = new Pair<Dog>(Milo, Fred);
Console.WriteLine("studentPair\t\t\t:{0}",
studentPair.ToString());
Console.WriteLine("dogPair\t\t\t\t:{0}",
dogPair.ToString()); 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("After Sort StudentPair\t\t:{0}",
studentPair.ToString());
studentPair.ReverseSort(theStudentDelegate); Console.WriteLine("After ReverseSort studentPair\t:{0}",
studentPair.ToString());
dogPair.Sort(theDogDelegate);
Console.WriteLine("After Sort dogPair\t\t:{0}",dogPair.ToString());
dogPair.ReverseSort(theDogDelegate);
Console.WriteLine("After ReverseSort dogPair\t:{0}",
dogPair.ToString());
Console.Read(); }
}
}
其中的: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 class Pair<T>,这个<T>是什么?集合吗?这里如何理解?
private T[],又如何理解?class Pair<T>类型的数组?
public delegate Comparison WhichIsFirst(T obj1, T obj2);这里的T又如何理解???
解决方案 »
- 散分!通过例子实现接口,继承,抽象类,虚方法的简单使用
- System.Timers.Timer怎么一次比一次运行的速度快?
- datagridview从数据库中读取网络图片地址并显示
- C#如何简单实现分页后单选框的checked属性不会改变
- winform 中如何画饼图,急急!
- 【求解】关于Net中的Mutex实现线程/进程互斥的问题....
- DataList获得ID问题
- 请教treeview的结点删除后,要把结点的TAG属性对应的对象一起删掉,该怎么做?对象不知道类型,急,谢谢
- C#水晶报表高手请进.怎么把GRIDVIEW里的数据放到水晶报表里..
- 主窗体为什么会被关闭???
- 怎样跟踪DLL中的值?
- 用C#做邮件收发
LZ去看下泛型相关的知识就知道了.