操作符重载和继承C# [英] operator overloading and inheritance c#

查看:137
本文介绍了操作符重载和继承C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个父类和子类,如下

Let's say I have a parent class and child class as below

父类:

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);
    }


}



子类:

Child Class:

  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);
    }

}

这是比较码孩子的两个实例。

And here is the code to compare two instances of child.

 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();



输出

真真假假

我知道为什么第一个比较期间给真正的和真实的。因为它调用父类的重载的==()操作符。我的问题是我想用子类==操作符,因为对象的类型是儿童,因此怎么可能?对于静态方法,虚拟关键字不能使用。

I know why it gives true and true during first comparison. Because it calls the overloaded ==() operator of parent class. My question is I wanted to use == operator of child class because the object is of type Child so how can it be possible? For static methods virtual keyword can not be used.

谢谢,

推荐答案

操作的实现在编译时选择。运营商不是虚拟方法 - 子类的 == 运营商没有覆盖父类 == 运营商。

The implementation of the operator is chosen at compile time. Operators are not virtual methods- the child class's == operator does not override the parent class == operator.

因此,为了使编译器选择子的唯一途径== 运营商是有变量本身是键入儿童,如:

Therefore the only way to make the compiler select the child == operator is to have the variables themselves be of type Child, e.g.

 Child p = new Child() { _First = "Mitul", _Last = "Patel", Address="abc1"};
 Child p1 = new Child() { _First = "Mitul", _Last = "Patel", Address = "abc" };

或有 == 话务员呼叫equals方法,使的Equals的儿童实施覆盖父执行:

or to have the == operator call the Equals method, and make the Child implementation of Equals override the parent implementation:

在Parent.cs:

in Parent.cs:

// 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);
}

这篇关于操作符重载和继承C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆