【发布时间】:2017-01-31 15:12:51
【问题描述】:
我像这样实现了CompareTo():
public override int CompareTo(object obj)
{
//quick failsafe
MyClass other;
if (obj is MyClass)
{
other = obj as MyClass;
}
else
{
return 1;
}
//now we should have another comparable object.
/*
* 1: this is greater.
* 0: equals.
* -1: this is less.
*/
if (other.GetValue() < this.GetValue())
{
// this is bigger
return 1;
}
else if (other.GetValue() > this.GetValue())
{
//this is smaller
return -1;
}
else
{
return 0;
}
}
但是,当我想选择函数 GetValue() 时,事情变得有趣起来。我为此设置了几个:即Average()、Best()、CorrectedAverage()、Median()。顺便说一下,我通过floats 的数组进行比较。问题是,我不想在我在此类中定义的enum 上使用switch-case 来告诉订购什么。有没有办法让我通过 nice and clean 来决定要订购哪个功能?
【问题讨论】: