【问题标题】:How to compare private data fields of objects to ensure they are the same (Java)?如何比较对象的私有数据字段以确保它们相同(Java)?
【发布时间】:2015-03-09 23:03:08
【问题描述】:

从这里开始,这是家庭作业/实验室,我正在寻求建议。我正在开发一个非常小的程序,它本质上是一个具有最小值/最大值约束的计数器和一个将值向上推的方法和另一个将值回滚到零 0 的方法。所以,我的 Counter 类的私有数据字段是:

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

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

这是他们给我的代码。

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

    }
    return result;
}

【问题讨论】:

  • 您只需比较实例变量的值,对对象使用等于,对基元使用“==”。当然,您首先需要将 otherObject 强制转换为您的类的实例。
  • Counter 中的任何方法都可以访问任何Counter 对象的私有变量。

标签: java class object boolean


【解决方案1】:

假设您的 equals 方法在 Counter 类中,它可以访问该类的所有私有成员,即使它们是该类的不同实例的成员。

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

【讨论】:

  • 可能有错误,boolean result = false; 如果 otherObject 不是 Counter 的实例,equals 方法应该返回 false。
  • 感谢您的帮助,以及所有提供答案的人。我很感谢您的帮助,并且一定会实施。
【解决方案2】:

实现equals-方法可能会很痛苦,尤其是当你的类中有很多属性时。

equals-method 状态的 JavaDoc

请注意,当hashCode 方法被重写时,通常需要重写此方法,以维护hashCode 方法的一般约定,即相等的对象必须具有相等的哈希码。

而且,如果您检查 JavaDoc 中的 hashCode-method

  • 如果两个对象根据equals(Object) 方法相等,则对这两个对象中的每一个调用hashCode 方法必须产生相同的整数结果。
  • 不要求如果两个对象根据equals(Object) 方法不相等,则对这两个对象中的每一个调用hashCode 方法必须产生不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同的整数结果可能会提高哈希表的性能。

因此,通常建议您实现这两种方法(equalshashCode)。下面显示了一种基于 Java 7 附带的java.util.Objects-class 的方法。方法Objects.equals(Object, Object) 处理空检查,这使得代码更简单、更易于阅读。此外,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);
}

【讨论】:

    【解决方案3】:

    由于equalsCounter的方法,你可以访问Counter的所有私有字段,所以你可以这样做:

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

    【讨论】:

      【解决方案4】:

      假设您的课程名为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;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多