6

以下のような親クラスと子クラスがあるとしましょう

親クラス:

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

}

そして、これが child の 2 つのインスタンスを比較するコードです。

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

出力

真 真 偽 偽

最初の比較で true と true が返される理由はわかっています。親クラスのオーバーロードされた ==() 演算子を呼び出すためです。私の質問は、オブジェクトが Child 型であるため、子クラスの == 演算子を使用したかったのですが、どうすれば可能ですか? 静的メソッドの場合、virtual キーワードは使用できません。

ありがとう、

4

2 に答える 2

12

演算子の実装は、コンパイル時に選択されます。演算子は仮想メソッドではありません。子クラスの==演算子は親クラスの演算子をオーバーライドしません==

したがって、コンパイラに子演算子を選択させる唯一の方法==は、変数自体を型Childにすることです。

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

または、==オペレーターに Equals メソッドを呼び出してもらい、Equals の Child 実装が親実装をオーバーライドするようにします。

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);
}
于 2012-05-09T20:34:22.557 に答える
1

staticメソッドは、実行時ではなくコンパイル時に解決されます。
あなたのコードではpp1Parentオブジェクトなので、クラスの==演算子を呼び出します。 派生クラスの演算子を呼び出す場合は、それらを派生クラス インスタンスとして宣言する必要があります。Parent

于 2012-05-09T20:34:31.083 に答える