using System;
using System.Collections;//注意using Collections
using System.Collections.Generic;
using System.Text;
namespace ZyyLove2008
{
class Program
{
static void Main(string[] args)
{
Person personA = new Person("zhang", 20);
Person personB = new Person("yang", 25);
int result = personA.CompareTo(personB);
//int result = personA.CompareTo(new object());//Error Argument '1': cannot convert from 'object' to 'ZyyLove2008.Person'
Console.WriteLine(result.ToString());
Console.Read();
}
}
public class Person : IComparable<Person>
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
private int _age;
public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
public Person(string name, int age)
{
this._name = name;
this._age = age;
}
#region IComparable<Person> Members
public int CompareTo(Person other)
{
return this.Age.CompareTo(other.Age);
}
#endregion
}
}
实现IComparable
最新推荐文章于 2020-03-07 19:24:43 发布
0
0
0
(请您对文章做出评价)