namespace Ch11Ex05
{
class Person : IComparable
{
public string Name;
public int Age;
public Person(string name, int age)
{
Name = name;
Age = age;
} public int CompareTo(object obj)
{
if (obj is Person)
{
Person otherPerson = obj as Person;
return this.Age - otherPerson.Age;
}
else
{
throw new ArgumentException(
"Object to compare to is not a Person object.");
}
}
}
}第二类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Ch11Ex05
{
public class PersonComparerName : IComparer
{
public static IComparer Default = new PersonComparerName(); public int Compare(object x, object y)
{
if (x is Person && y is Person)
{
return Comparer.Default.Compare(
((Person)x).Name, ((Person)y).Name);
}
else
{
throw new ArgumentException(
"One or both objects to compare are not Person objects.");
}
}
}
}主函数代码using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Ch11Ex05
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person("Jim", 30));
list.Add(new Person("Bob", 25));
list.Add(new Person("Bert", 27));
list.Add(new Person("Ernie", 22)); Console.WriteLine("Unsorted people:");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})",
(list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine(); Console.WriteLine(
"People sorted with default comparer (by age):");
list.Sort();
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})",
(list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine(); Console.WriteLine(
"People sorted with nondefault comparer (by name):");
list.Sort(PersonComparerName.Default);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})",
(list[i] as Person).Name, (list[i] as Person).Age);
} Console.ReadKey();
}
}
}
问题:定义了person类中的CompareTo()方法
为什么其他类或者方法都没有调用它?它实现了什么功能,求解 谢谢!
{
class Person : IComparable
{
public string Name;
public int Age;
public Person(string name, int age)
{
Name = name;
Age = age;
} public int CompareTo(object obj)
{
if (obj is Person)
{
Person otherPerson = obj as Person;
return this.Age - otherPerson.Age;
}
else
{
throw new ArgumentException(
"Object to compare to is not a Person object.");
}
}
}
}第二类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Ch11Ex05
{
public class PersonComparerName : IComparer
{
public static IComparer Default = new PersonComparerName(); public int Compare(object x, object y)
{
if (x is Person && y is Person)
{
return Comparer.Default.Compare(
((Person)x).Name, ((Person)y).Name);
}
else
{
throw new ArgumentException(
"One or both objects to compare are not Person objects.");
}
}
}
}主函数代码using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Ch11Ex05
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person("Jim", 30));
list.Add(new Person("Bob", 25));
list.Add(new Person("Bert", 27));
list.Add(new Person("Ernie", 22)); Console.WriteLine("Unsorted people:");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})",
(list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine(); Console.WriteLine(
"People sorted with default comparer (by age):");
list.Sort();
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})",
(list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine(); Console.WriteLine(
"People sorted with nondefault comparer (by name):");
list.Sort(PersonComparerName.Default);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})",
(list[i] as Person).Name, (list[i] as Person).Age);
} Console.ReadKey();
}
}
}
问题:定义了person类中的CompareTo()方法
为什么其他类或者方法都没有调用它?它实现了什么功能,求解 谢谢!
我觉得是list[i] as Person 隐式调用了CompareTo()方法
使用每个元素的 System.IComparable 实现对整个 System.Collections.ArrayList 中的元素进行排序。
使用每个元素对象自己实现的CompareTo方法来比较,如果不实现的话会报错。list.Sort(PersonComparerName.Default);
使用指定的比较器对整个 System.Collections.ArrayList 中的元素进行排序。
使用指定的比较器来比较对象。
http://msdn.microsoft.com/zh-cn/library/system.icomparable.compareto(v=vs.80).aspx