【发布时间】:2017-02-28 22:37:18
【问题描述】:
我正在开发一个程序,它是一个更大程序的一部分,但我创建了这个简单的应用程序来演示我遇到的问题。这是我的代码:
using System;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
var bear = new Animal("Bear");
var bear2 = new Animal("Bear");
var dog = new Dog("Woof!", "Dog");
var dog2 = new Dog("Bow-Wow!", "Dog");
AreEqual(bear, bear2);
AreEqual(dog2, dog);
}
private static void AreEqual(Animal animal1, Animal animal2)
{
if (animal1 == animal2)
Console.WriteLine($"{animal1,15} == {animal2}");
else
Console.WriteLine($"{animal1,15} != {animal2}");
}
}
public sealed class Dog : Animal
{
public string Action { get; }
public static bool operator ==(Dog x, Dog y)
{
return x.Name == y.Name && x.Action == y.Action;
}
public static bool operator !=(Dog x, Dog y)
{
return !(x.Name == y.Name && x.Action == y.Action);
}
public override bool Equals(object obj)
{
if (!base.Equals(obj))
{
return false;
}
Dog rhs = (Dog)obj;
return Action == rhs.Action;
}
public override int GetHashCode()
{
return base.GetHashCode() ^ Action.GetHashCode();
}
public Dog(string action, string animalName) : base(animalName)
{
Action = action;
}
public override string ToString()
{
return $"{Name} ({Action})";
}
}
public class Animal
{
public string Name { get; }
public Animal(string animalName)
{
Name = animalName;
}
public static bool operator ==(Animal x, Animal y)
{
return x.Name == y.Name;
}
public static bool operator !=(Animal x, Animal y)
{
return x.Name != y.Name;
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (ReferenceEquals(obj, this))
return true;
if (obj.GetType() != GetType())
return false;
Animal rhs = obj as Animal;
return Name == rhs.Name;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
public override string ToString()
{
return Name;
}
}
}
当我运行程序时,我在屏幕上看到了这个:
Bear == Bear
Dog (Bow-wow!) == Dog (Woof!)
这是不正确的。我期待看到的是:
Bear == Bear
Dog (Bow-wow!) != Dog (Woof!)
在Dog 类中,我覆盖了比较Name 字段和Action 字段的== 运算符。显然 Action 字段对于每个实例都是不同的,所以我不明白为什么说它们是相等的。任何帮助,将不胜感激。我确定这是我忽略的一些简单的事情。
【问题讨论】:
-
你能在调试会话中单步调试代码吗?
-
该方法在
Animals上运行,而不是Dogs。 -
@pinkfloydx33 - 正确,但不应该像
.ToString()那样多态性接管并调用正确的方法? -
如果您希望相等运算符考虑对象的运行时类型,您可以将方法签名更改为接受
dynamic -
不,因为相等运算符是静态方法,当然不像您的
ToString方法那样虚拟。
标签: c# inheritance operator-overloading