如何在我的结构中实现 IComparable<Time>?
How to implement IComparable<Time> in my Struct?
我的代码包含名为 Time
的结构。
我想在我的结构中实现 IComparable<Time>
接口来比较 'Time'.
类型的两个变量
我已经用 IEquatable
做到了并且它有效,但我不知道如何用 IComparable
做到这一点。
由于 Time
由三个变量(小时、分钟、秒)组成,我不能简单地考虑 if
中的每种情况。或者我可以吗?你能给我一个如何编码的提示吗?谢谢。
{
public struct Time : IEquatable<Time>, IComparable<Time>
{
private byte hours, minutes, seconds;
public byte Hours { get { return hours; } }
public byte Minutes { get { return minutes; } }
public byte Seconds { get { return seconds; } }
public Time(byte hh, byte mm, byte ss)
{
if (hh < 0 || hh > 24)
throw new ArgumentOutOfRangeException();
if (mm < 0 || mm > 60)
throw new ArgumentOutOfRangeException();
if (ss < 0 || ss > 60)
throw new ArgumentOutOfRangeException();
this.hours = hh;
this.minutes = mm;
this.seconds = ss;
}
public Time(byte hh, byte mm) : this(hh, mm, default(byte)) { }
public Time(byte hh) : this(hh, default(byte), default(byte)) { }
public Time(string hms)
{
string[] arr = hms.Split(':');
this.hours = Convert.ToByte(arr[0]);
this.minutes = Convert.ToByte(arr[1]);
this.seconds = Convert.ToByte(arr[2]);
}
public override string ToString()
{
return Hours + ":" + Minutes + ":" + Seconds;
}
public override bool Equals(object t)
{
if (t == null || this.GetType() != t.GetType()) return false;
return (this.Hours == ((Time)t).Hours &&
this.Minutes == ((Time)t).Minutes &&
this.Seconds == ((Time)t).Seconds);
}
public override int GetHashCode()
{
return this.GetHashCode();
}
// IEquatable
public bool Equals(Time other)
{
return this.Equals((object)other);
}
public static bool operator == (Time t1, Time t2)
{
return t1.Equals(t2);
}
public static bool operator !=(Time t1, Time t2)
{
return !t1.Equals(t2);
}
// IComparable
public int CompareTo(Time other)
{
if (this == other)
return 0;
// Code that compares two variables
}
public static bool operator > (Time t1, Time t2)
{
return t1.CompareTo(t2) == 1;
}
public static bool operator < (Time t1, Time t2)
{
return t1.CompareTo(t2) == -1;
}
public static bool operator >= (Time t1, Time t2)
{
return t1.CompareTo(t2) >= 0;
}
public static bool operator <= (Time t1, Time t2)
{
return t1.CompareTo(t2) <= 0;
}
}
}
public int CompareTo(Time other)
{
var by_hour = this.Hours - other.Hours;
if (by_hour != 0)
return by_hour;
var by_minute = this.Minutes - other.Minutes;
if (by_minute != 0)
return by_minute;
return this.Seconds - other.Seconds;
}
同时更改所有使用 Compare
方法的地方,以说明 it can return 除了 1 和 -1 之外的非零值。
我的代码包含名为 Time
的结构。
我想在我的结构中实现 IComparable<Time>
接口来比较 'Time'.
我已经用 IEquatable
做到了并且它有效,但我不知道如何用 IComparable
做到这一点。
由于 Time
由三个变量(小时、分钟、秒)组成,我不能简单地考虑 if
中的每种情况。或者我可以吗?你能给我一个如何编码的提示吗?谢谢。
{
public struct Time : IEquatable<Time>, IComparable<Time>
{
private byte hours, minutes, seconds;
public byte Hours { get { return hours; } }
public byte Minutes { get { return minutes; } }
public byte Seconds { get { return seconds; } }
public Time(byte hh, byte mm, byte ss)
{
if (hh < 0 || hh > 24)
throw new ArgumentOutOfRangeException();
if (mm < 0 || mm > 60)
throw new ArgumentOutOfRangeException();
if (ss < 0 || ss > 60)
throw new ArgumentOutOfRangeException();
this.hours = hh;
this.minutes = mm;
this.seconds = ss;
}
public Time(byte hh, byte mm) : this(hh, mm, default(byte)) { }
public Time(byte hh) : this(hh, default(byte), default(byte)) { }
public Time(string hms)
{
string[] arr = hms.Split(':');
this.hours = Convert.ToByte(arr[0]);
this.minutes = Convert.ToByte(arr[1]);
this.seconds = Convert.ToByte(arr[2]);
}
public override string ToString()
{
return Hours + ":" + Minutes + ":" + Seconds;
}
public override bool Equals(object t)
{
if (t == null || this.GetType() != t.GetType()) return false;
return (this.Hours == ((Time)t).Hours &&
this.Minutes == ((Time)t).Minutes &&
this.Seconds == ((Time)t).Seconds);
}
public override int GetHashCode()
{
return this.GetHashCode();
}
// IEquatable
public bool Equals(Time other)
{
return this.Equals((object)other);
}
public static bool operator == (Time t1, Time t2)
{
return t1.Equals(t2);
}
public static bool operator !=(Time t1, Time t2)
{
return !t1.Equals(t2);
}
// IComparable
public int CompareTo(Time other)
{
if (this == other)
return 0;
// Code that compares two variables
}
public static bool operator > (Time t1, Time t2)
{
return t1.CompareTo(t2) == 1;
}
public static bool operator < (Time t1, Time t2)
{
return t1.CompareTo(t2) == -1;
}
public static bool operator >= (Time t1, Time t2)
{
return t1.CompareTo(t2) >= 0;
}
public static bool operator <= (Time t1, Time t2)
{
return t1.CompareTo(t2) <= 0;
}
}
}
public int CompareTo(Time other)
{
var by_hour = this.Hours - other.Hours;
if (by_hour != 0)
return by_hour;
var by_minute = this.Minutes - other.Minutes;
if (by_minute != 0)
return by_minute;
return this.Seconds - other.Seconds;
}
同时更改所有使用 Compare
方法的地方,以说明 it can return 除了 1 和 -1 之外的非零值。