overriding 的效果是暂时的还是永久的?
is overriding's effect temporary or permanently?
我在我的一个程序中编写了这些代码行,用于改变 class 的两个实例之间的相等性比较,从它们的引用到它们的值:
public interface IEquatable<Clock>
{
}
public bool Equals(Clock theObj)
{
if (theObj == null)
{
return false;
}
if (theObj.ToString() == this.ToString())
{
return true;
}
else
{
return false;
}
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
Clock clck = obj as Clock;
if (clck == null)
{
return false;
}
else
{
return Equals(clck);
}
}
public override int GetHashCode()
{
return Hours;
}`
现在,当我尝试在不同解决方案的不同项目中使用“Equals(T)”方法比较 class 的两个实例时,它比较的是它们的值而不是它们的参考值。
覆盖一个项目中的方法是否会影响其他项目中的该方法?
我的完整代码是:
`使用系统;
使用 System.Diagnostics.Contracts;
使用 System.Runtime.InteropServices.WindowsRuntime;
使用 System.Security.Cryptography.X509Certificates;
public class Clock
{
public int Minutes;
public int Hours;
public interface IEquatable<Clock>
{
}
public bool Equals(Clock theObj)
{
if (theObj == null)
{
return false;
}
if (theObj.ToString() == this.ToString())
{
return true;
}
else
{
return false;
}
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
Clock clck = obj as Clock;
if (clck == null)
{
return false;
}
else
{
return Equals(clck);
}
}
public override int GetHashCode()
{
return Hours;
}
public override string ToString()
{
if (Hours < 10)
{
if (Minutes < 10)
{
return "0" + Hours + ":0" + Minutes;
}
else
{
return "0" + Hours + ":" + Minutes;
}
}
else
{
if (Minutes < 10)
{
return Hours + ":0" + Minutes;
}
else
{
return Hours + ":" + Minutes;
}
}
}
public Clock(int hours, int minutes)
{
while (minutes > 59 || minutes < 0)
{
if (minutes > 59)
{
minutes -= 60;
hours++;
}
if (minutes < 0)
{
minutes += 60;
hours--;
}
}
while (hours > 23 || hours < 0)
{
if (hours > 23)
{
hours -= 24;
}
if (hours < 0)
{
hours += 24;
}
}
Hours = hours;
Minutes = minutes;
}
public Clock Add(int minutesToAdd)
{
var res = new Clock(Hours , Minutes + minutesToAdd);
return res;
}
public Clock Subtract(int minutesToSubtract)
{
var res = new Clock(Hours , Minutes - minutesToSubtract);
return res;
}}
感谢您的宝贵时间!
你的Equals
方法逻辑错误!对于 类 没有 ToString
方法覆盖,输出是这样的 WhosebugCodes.Clock
,这导致所有对象都相等!
请查看以下代码以获得更多想法:
public class Clock : IEquatable<Clock>
{
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public bool Equals(Clock c)
{
if (c == null)
return false;
// Here is a somewhat correct logic for equality (sort of ...)
//return c.Hours == Hours && c.Minutes == Minutes;
return c.ToString() == this.ToString();
}
public override int GetHashCode() => Hours * 3600 + Minutes * 60 + Seconds;
public override bool Equals(object obj)
{
var clock = obj as Clock;
if (clock != null)
return Equals(clock);
return false;
}
public static void Main()
{
Clock c2 = new Clock { Hours = 12, Minutes = 01, Seconds = 59 };
var c1 = new Clock { Hours = 12 };
Console.WriteLine(c1.Equals(c2));
Console.WriteLine(Equals(c1, c2));
Console.WriteLine(c1 == c2);
Console.WriteLine(c1.ToString());
Console.WriteLine(c2.ToString());
}
对于您的问题,如果您引用的 project/solution 具有您定义的 Clock
类型,那么您正在使用您定义的自定义逻辑,否则它们不应该有任何共同点。
我在我的一个程序中编写了这些代码行,用于改变 class 的两个实例之间的相等性比较,从它们的引用到它们的值:
public interface IEquatable<Clock>
{
}
public bool Equals(Clock theObj)
{
if (theObj == null)
{
return false;
}
if (theObj.ToString() == this.ToString())
{
return true;
}
else
{
return false;
}
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
Clock clck = obj as Clock;
if (clck == null)
{
return false;
}
else
{
return Equals(clck);
}
}
public override int GetHashCode()
{
return Hours;
}`
现在,当我尝试在不同解决方案的不同项目中使用“Equals(T)”方法比较 class 的两个实例时,它比较的是它们的值而不是它们的参考值。
覆盖一个项目中的方法是否会影响其他项目中的该方法?
我的完整代码是: `使用系统; 使用 System.Diagnostics.Contracts; 使用 System.Runtime.InteropServices.WindowsRuntime; 使用 System.Security.Cryptography.X509Certificates;
public class Clock
{
public int Minutes;
public int Hours;
public interface IEquatable<Clock>
{
}
public bool Equals(Clock theObj)
{
if (theObj == null)
{
return false;
}
if (theObj.ToString() == this.ToString())
{
return true;
}
else
{
return false;
}
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
Clock clck = obj as Clock;
if (clck == null)
{
return false;
}
else
{
return Equals(clck);
}
}
public override int GetHashCode()
{
return Hours;
}
public override string ToString()
{
if (Hours < 10)
{
if (Minutes < 10)
{
return "0" + Hours + ":0" + Minutes;
}
else
{
return "0" + Hours + ":" + Minutes;
}
}
else
{
if (Minutes < 10)
{
return Hours + ":0" + Minutes;
}
else
{
return Hours + ":" + Minutes;
}
}
}
public Clock(int hours, int minutes)
{
while (minutes > 59 || minutes < 0)
{
if (minutes > 59)
{
minutes -= 60;
hours++;
}
if (minutes < 0)
{
minutes += 60;
hours--;
}
}
while (hours > 23 || hours < 0)
{
if (hours > 23)
{
hours -= 24;
}
if (hours < 0)
{
hours += 24;
}
}
Hours = hours;
Minutes = minutes;
}
public Clock Add(int minutesToAdd)
{
var res = new Clock(Hours , Minutes + minutesToAdd);
return res;
}
public Clock Subtract(int minutesToSubtract)
{
var res = new Clock(Hours , Minutes - minutesToSubtract);
return res;
}}
感谢您的宝贵时间!
你的Equals
方法逻辑错误!对于 类 没有 ToString
方法覆盖,输出是这样的 WhosebugCodes.Clock
,这导致所有对象都相等!
请查看以下代码以获得更多想法:
public class Clock : IEquatable<Clock>
{
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public bool Equals(Clock c)
{
if (c == null)
return false;
// Here is a somewhat correct logic for equality (sort of ...)
//return c.Hours == Hours && c.Minutes == Minutes;
return c.ToString() == this.ToString();
}
public override int GetHashCode() => Hours * 3600 + Minutes * 60 + Seconds;
public override bool Equals(object obj)
{
var clock = obj as Clock;
if (clock != null)
return Equals(clock);
return false;
}
public static void Main()
{
Clock c2 = new Clock { Hours = 12, Minutes = 01, Seconds = 59 };
var c1 = new Clock { Hours = 12 };
Console.WriteLine(c1.Equals(c2));
Console.WriteLine(Equals(c1, c2));
Console.WriteLine(c1 == c2);
Console.WriteLine(c1.ToString());
Console.WriteLine(c2.ToString());
}
对于您的问题,如果您引用的 project/solution 具有您定义的 Clock
类型,那么您正在使用您定义的自定义逻辑,否则它们不应该有任何共同点。