日期:2014-05-17 浏览次数:21030 次
sealed class Rectangle :IComparable
{
public double Length { get; set; }
public double Width { get; set; }
public Rectangle()
{ }
public Rectangle(double length, double width)
{
this.Length = length;
this.Width = width;
}
public double GetArea()
{
return this.Length * this.Width;
}
int IComparable.CompareTo(object obj)
{
if (obj is Rectangle)
return this.GetArea().CompareTo(((Rectangle)obj).GetArea());
throw new ArgumentException("参数obj不是Rectangle的实例");
}
}
class Program
{
static void Main(string[] args)
{
IComparable r1 = new Rectangle(2, 2);
IComparable r2 = "hello world";
Console.WriteLine(r1.CompareTo(r2));
}
}