如何比较对象的私有数据字段以确保它们相同(Java)?

How to compare private data fields of objects to ensure they are the same (Java)?

从这里开始,这是 homework/a 实验室,我正在寻求建议。我正在开发一个非常小的程序,它本质上是一个具有 min/max 值约束的计数器和一个将值向上推的方法,另一个将值回滚到零 0。因此,我的私有数据字段计数器 class 是:

private int minimum; 
private int maximum; 
private int currentValue; 

我在这里遇到的问题是使用一种方法将我的计数器 Class 与另一个基于相同 class 的理论对象进行比较。在这种情况下,我们希望看到两个对象之间的数据字段相同。我研究了几种方法来做到这一点,包括使用反射和著名的 EqualsBuilder,但我在实现每一种方法时都遇到了麻烦。

这是他们给我的代码。

public boolean equals(Object otherObject)
{
    boolean result = true;
    if (otherObject instanceof Counter)
    {

    }
    return result;
}

假设您的 equals 方法在计数器 class 中,它可以访问该 class 的所有私有成员,即使它们是该 [=13= 的不同实例的成员].

public boolean equals(Object otherObject)
{
    if (otherObject instanceof Counter)
    {
        Counter ocounter = (Counter) otherObject;
        if (this.minimum != ocounter.minimum)
            return false;
        ...
    } else {
        return false;
    }
    return true;
}

因为equalsCounter的一个方法,你可以访问Counter的所有私有字段,所以你可以这样做:

if (otherObject instanceof Counter)
{
    if (this.minimum != ((Counter) otherObject).minimum) {
        result = false;
    }
    // [...]
}

假设您的 class 被称为 Counter 并且您为所有私有字段创建了 getters(您应该这样做):

    @Override 
    public boolean equals(Object other) {
        boolean result = false;
        if (other instanceof Counter) {
            Counter c= (Counter) other;
            result = (this.getMinimum() == that.getMinimum() &&
                     this.getMaximum() == that.getMaximum() &&
                     this.getCurrentValue() == that.getCurrentValue());
        }
        return result;
    }

实施 equals 方法可能真的很痛苦,尤其是当您的 class.

中有很多属性时

Javaequals-method 个州的文档

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

并且,如果您查看 Java文档中的 hashCode-method

  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

因此,通常建议您实施这两种方法(equalshashCode)。下面显示了一种基于 Java 附带的 java.util.Objects-class 7 的方法。方法 Objects.equals(Object, Object) handles null checks which makes the code simpler and easier to read. Furthermore, the hash-method 是一种创建值的便捷方法,可以与 hashCode.

一起使用

所以,回答你的问题。要访问 other 对象的属性,只需执行类型转换即可。之后您可以访问 other 对象的私有属性。但是,请记住在使用 instanceof.

检查类型后始终执行此操作
@Override
public boolean equals(Object other) {
    if (other instanceof Counter) { // Always check the type to be safe
        // Cast to a Counter-object
        final Counter c = (Counter) other;

        // Now, you can access the private properties of the other object
        return Objects.equals(minimum, c.minimum) &&
               Objects.equals(maximum, c.maximum) &&
               Objects.equals(currentValue, c.currentValue);
    }
    return false; // If it is not the same type, always return false
}

@Override
public int hashCode() {
    return Objects.hash(currentValue, maximum, minimum);
}