什么是"最佳实践"为了比较的参考类型两个实例?实例、两个、类型、QUOT

2023-09-02 01:20:31 作者:中国移不动

我碰到这次来到最近,到现在为止,我一直愉快地重写相等运算符( == )和/或等于方法,以两个引用是否看到类型实际上包含相同的数据(即两个不同的式样差不多的情况下)。

我一直用这个更因为我已经越来越多在​​自动化测试(比较基准/针对该预期的返回数据)。

虽然找了一些编码标准的指导方针在MSDN 我的碰到一个文章是反对这一提议。现在我明白的为什么的文章是这样说的(因为他们是不一样的实例的),但它并没有回答这个问题:

什么是比较两个引用类型的最好方法是什么? 如果我们实行 IComparable的? (我还看到一提,这应该留给唯一的值类型)。 有没有一些界面我不知道? 如果我们刚刚推出我们自己的?!

非常感谢^ _ ^

更新

貌似我错看了一些文件(这是一个漫长的一天),并覆盖的等于可能是要走的路。

  

如果您正在实施的参考   类型,你应该考虑覆盖   equals方法的引用类型   如果你的类型看起来像一个基本类型   如一个点,弦乐,BigNumber,   等等。大多数引用类型应   不超载的平等运营商,   即使如果他们重载Equals 。然而,   如果要实现一个参考   类型意图具有价值   语义,比如一个复数   类型,你应该重写平等   运营商。

解决方案

它看起来像你的编码在C#中,其中有一个名为方法Equals您的类应该实现,如果你想使用一些其他的指标来比较两个对象比是这两个指针(因为对象句柄是正义的,指针),以相同的内存地址吗?

我一把抓起这里一些样本code:

 类TwoDPoint:System.Object的
{
    公共只读INT X,Y;

    公共TwoDPoint(INT X,int y)对//构造
    {
        this.x = X;
        this.y = Y;
    }

    公众覆盖布尔等于(System.Object的OBJ)
    {
        //如果参数为null,返回false。
        如果(OBJ == NULL)
        {
            返回false;
        }

        //如果参数不能转换为点返回false。
        TwoDPoint P = OBJ为TwoDPoint;
        如果((System.Object的)p == NULL)
        {
            返回false;
        }

        //返回true,如果该字段匹配:
        则返回(x == p.x)及和放大器; (Y == p.y);
    }

    公共布尔等于(TwoDPoint P)
    {
        //如果参数为空返回错误:
        如果((对象)p == NULL)
        {
            返回false;
        }

        //返回true,如果该字段匹配:
        则返回(x == p.x)及和放大器; (Y == p.y);
    }

    公众覆盖INT GetHash code()
    {
        返回X ^ÿ;
    }
}
 

Java有非常类似的机制。在等于()的方法的一部分的对象的类,和你的类重载它,如果你想这种类型的功能。

原因重载==可以为对象的一个​​坏主意的是,通常情况下,你仍然希望能够做到这些都是同一指针的比较。这些通常依靠的,例如,将元素插入一个列表,其中允许无重复的,如果这个操作符重载在一个非标准的方式你的一些框架的东西可能无法正常工作。

互联网技术经理工作最佳实践

I came across this recently, up until now I have been happily overriding the equality operator (==) and/or Equals method in order to see if two references types actually contained the same data (i.e. two different instances that look the same).

I have been using this even more since I have been getting more in to automated testing (comparing reference/expected data against that returned).

While looking over some of the coding standards guidelines in MSDN I came across an article that advises against it. Now I understand why the article is saying this (because they are not the same instance) but it does not answer the question:

What is the best way to compare two reference types? Should we implement IComparable? (I have also seen mention that this should be reserved for value types only). Is there some interface I don't know about? Should we just roll our own?!

Many Thanks ^_^

Update

Looks like I had mis-read some of the documentation (it's been a long day) and overriding Equals may be the way to go..

If you are implementing reference types, you should consider overriding the Equals method on a reference type if your type looks like a base type such as a Point, String, BigNumber, and so on. Most reference types should not overload the equality operator, even if they override Equals. However, if you are implementing a reference type that is intended to have value semantics, such as a complex number type, you should override the equality operator.

解决方案

It looks like you're coding in C#, which has a method called Equals that your class should implement, should you want to compare two objects using some other metric than "are these two pointers (because object handles are just that, pointers) to the same memory address?".

I grabbed some sample code from here:

class TwoDPoint : System.Object
{
    public readonly int x, y;

    public TwoDPoint(int x, int y)  //constructor
    {
        this.x = x;
        this.y = y;
    }

    public override bool Equals(System.Object obj)
    {
        // If parameter is null return false.
        if (obj == null)
        {
            return false;
        }

        // If parameter cannot be cast to Point return false.
        TwoDPoint p = obj as TwoDPoint;
        if ((System.Object)p == null)
        {
            return false;
        }

        // Return true if the fields match:
        return (x == p.x) && (y == p.y);
    }

    public bool Equals(TwoDPoint p)
    {
        // If parameter is null return false:
        if ((object)p == null)
        {
            return false;
        }

        // Return true if the fields match:
        return (x == p.x) && (y == p.y);
    }

    public override int GetHashCode()
    {
        return x ^ y;
    }
}

Java has very similar mechanisms. The equals() method is part of the Object class, and your class overloads it if you want this type of functionality.

The reason overloading '==' can be a bad idea for objects is that, usually, you still want to be able to do the "are these the same pointer" comparisons. These are usually relied upon for, for instance, inserting an element into a list where no duplicates are allowed, and some of your framework stuff may not work if this operator is overloaded in a non-standard way.