操作符重载和继承C#
问题描述:
比方说,我有一个父类和子类,如下操作符重载和继承C#
父类:
class Parent
{
public string _First;
public string _Last;
public override bool Equals(object obj)
{
if (ReferenceEquals(obj, null))
return false;
else if (ReferenceEquals(obj, this))
return true;
else if (obj is Parent == false)
return false;
else
return this.Equals(obj as Parent) & base.Equals(obj);
}
public override int GetHashCode()
{
unchecked
{
return this._First.GetHashCode()^this._Last.GetHashCode()^base.GetHashCode();
}
}
public bool Equals(Parent that)
{
if (ReferenceEquals(that, null))
return false;
else if (ReferenceEquals(that, this))
return true;
else
return this._First.Equals(that._First) & this._Last.Equals(that._Last);
}
public static bool operator ==(Parent p1, Parent p2)
{
return p1.Equals(p2);
}
public static bool operator !=(Parent p1, Parent p2)
{
return !p1.Equals(p2);
}
}
子类:
class Child : Parent
{
public string Address;
public override bool Equals(object obj)
{
if (ReferenceEquals(obj, null))
return false;
else if (ReferenceEquals(obj, this))
return true;
else if (obj is Parent == false)
return false;
else
return this.Equals(obj as Child);
}
public override int GetHashCode()
{
unchecked
{
return this._First.GetHashCode()^this._Last.GetHashCode()^base.GetHashCode();
}
}
public bool Equals(Child that)
{
if (ReferenceEquals(that, null))
return false;
else if (ReferenceEquals(that, this))
return true;
else
return this.Address.Equals(that.Address) & base.Equals(that);
}
public static bool operator ==(Child p1,Child p2)
{
return p1.Equals(p2);
}
public static bool operator !=(Child p1, Child p2)
{
return !p1.Equals(p2);
}
}
这里是代码比较两个孩子的情况。
Parent p = new Child() { _First = "Mitul", _Last = "Patel", Address="abc1"};
Parent p1 = new Child() { _First = "Mitul", _Last = "Patel", Address = "abc" };
Child c = new Child() { _First = "Mitul", _Last = "Patel", Address = "abc1" };
Child c1 = new Child() { _First = "Mitul", _Last = "Patel", Address = "abc" };
Console.WriteLine(p.Equals(p1));
Console.WriteLine(p == p1);
Console.WriteLine(c.Equals(c1));
Console.WriteLine(c == c1);
Console.Read();
输出
真真假假
我知道为什么第一个比较过程中提供真正的和真实的。因为它调用父类的重载==()运算符。我的问题是我想使用子类的==操作符,因为对象是Child类型,所以它怎么可能?对于静态方法,virtual关键字不能使用。
谢谢,
答
在编译时选择运算符的实现。运算符不是虚方法 - 子类的==
运算符不会覆盖父类==
运算符。因此,编译器选择子操作符==
的唯一方法是让变量本身的类型为Child
,例如,
Child p = new Child() { _First = "Mitul", _Last = "Patel", Address="abc1"};
Child p1 = new Child() { _First = "Mitul", _Last = "Patel", Address = "abc" };
或有==
操作员呼叫Equals方法,使的Equals的儿童实现覆盖父类的实现:
:
// No need for these to be public- they should only be called internally.
protected virtual bool Equals(Parent that)
{
if (ReferenceEquals(that, null))
return false;
else if (ReferenceEquals(that, this))
return true;
else
return this._First.Equals(that._First) & this._Last.Equals(that._Last);
}
在Child.cs :
// Notice I changed your argument type here...
protected override bool Equals(Parent that)
{
// If we are not dealing with a Child instance, delegate to the base class.
if (!(that is typeof(Child)))
return base.Equals(that);
if (ReferenceEquals(that, null))
return false;
else if (ReferenceEquals(that, this))
return true;
else
return this.Address.Equals(that.Address) & base.Equals(that);
}
答
static
方法在co mpile时间,而不是在运行时间。
对于您的代码p
和p1
是Parent
对象,所以它会调用Parent
类的==
运算符。
如果您希望调用派生类的运算符,则必须将它们声明为派生类实例。
谢谢克里斯。是的,这有意义使Equals方法在父项中是虚拟的。 – mchicago