namespace Operators
{
struct Minute
{
public Minute(int initialValue)
{
this.value = System.Math.Abs(initialValue) % 60;
}
public int Value
{
get { return this.value; }
}
public static bool operator ==(Minute lhs, Minute rhs)
{
return lhs.value == rhs.value;
}
public static bool operator ==(Minute lhs, int rhs)
{
return lhs.value == rhs;
}
public static bool operator ==(int lhs, Minute rhs)
{
return lhs == rhs.value;
}
public static bool operator !=(Minute lhs, Minute rhs)
{
return lhs.value != rhs.value;
}
public static bool operator !=(Minute lhs, int rhs)
{
return lhs.value != rhs;
}
public static bool operator !=(int lhs, Minute rhs)
{
return lhs != rhs.value;
}
/// 代码段一
public override bool Equals(object other)
{
return (other is Minute) && Equals((Minute)other);
}
/// 代码段二
public bool Equals(Minute other)
{
return this.value == other.value;
}
/// 代码段三
public override int GetHashCode()
{
return this.value;
}
public static Minute operator++(Minute arg)
{
arg.value++;
if (arg.value == 60)
{
arg.value = 0;
}
return arg;
}
private int value;
}
}
问题一(红色部份的代码):像这样override一个方法有实际意义吗?与直接写一个Equals方法(如代码段一)应能完成吧.
问题二(绿色部份的代码):该方法是否每次都返回一个相同的值,即当前的value变量的值