如何在java中正确覆盖equals继承?

时间:2012-03-27 08:24:14

标签: java equals guava

我正在使用hibernate和id ...用于持久性(这就是为什么它在比较中被省略)。 (另外,使用google guava helper equals)

HolidayPackageVariant:

public abstract class HolidayPackageVariant {
    private Integer idHolidayPackageVariant;
    private HolidayPackage holidayPackage;
    private String typeHolidayPackage;

@Override
public boolean equals(Object obj) {
    if (obj == this)
        return true;
    if(obj == null)
        return false;

    if (getClass().equals(obj.getClass())) {
        final HolidayPackageVariant otherPackageVariant = (HolidayPackageVariant) obj;
        return Objects.equal(getTypeHolidayPackage(),otherPackageVariant.getTypeHolidayPackage())
                && Objects.equal(getHolidayPackage(),
                        otherPackageVariant.getHolidayPackage());
    }
    return false;
}

@Override
public int hashCode() {
    return Objects.hashCode(getTypeHolidayPackage(), getHolidayPackage());
}

FlightHolidayPackageVariant:

public final class FlightHolidayPackageVariant extends HolidayPackageVariant{
    private Destination originCity;
    public boolean equals(Object obj) {
    // .. 

我应该完全覆盖equals()还是应该以某种方式调用super.equals(...)?

2 个答案:

答案 0 :(得分:3)

这是你应该如何实现数据对象:不允许继承,只有组合。

这些是不可变对象,但您可能想要修改它们,因此请在需要时删除final。您也可以从类定义中删除final,因为Hibernate不支持它,但在这种情况下,您应该记录这些类不符合继承条件。

主要优点是Effective Java plus中描述的所有优点,在这种情况下,您不必通过Hibernate管理继承,这有时可能是一种真正的痛苦。

public final class HolidayPackageVariant {
  private final Integer idHolidayPackageVariant;
  private final HolidayPackage holidayPackage;
  private final String typeHolidayPackage;

  ...

  @Override
  public boolean equals(Object obj) {
    if (obj == this)
      return true;
    if (!(obj instanceof HolidayPackageVariant))
      return false;

    HolidayPackageVariant that = (HolidayPackageVariant) obj;
    return Objects.equal(this.typeHolidayPackage, that.typeHolidayPackage)
        && Objects.equal(this.holidayPackage, that.holidayPackage);
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(this.typeHolidayPackage, this.holidayPackage);
  }
}

public final class FlightHolidayPackageVariant {
  private HolidayPackageVariant holidayPackageVariant;
  private Destination originCity;

  ...

  public HolidayPackageVariant asHolidayPackageVariant() {
    return this.holidayPackageVariant;
  }

  public boolean equals(Object obj) {
    if (obj == this)
      return true;
    if (!(obj instanceof FlightHolidayPackageVariant))
      return false;

    FlightHolidayPackageVariant that = (FlightHolidayPackageVariant) obj;
    return Objects.equal(this.holidayPackageVariant, that.holidayPackageVariant)
        && Objects.equal(this.originCity, that.originCity);
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(this.holidayPackageVariant, this.originCity);
  }
}

答案 1 :(得分:2)

关注Secret of Equals

HolidayPackageVariant:

public abstract class HolidayPackageVariant {
    private Integer idHolidayPackageVariant;
    private HolidayPackage holidayPackage;
    private String typeHolidayPackage;

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if(obj == null) return false;

        if (getClass().equals(obj.getClass())) {
            final HolidayPackageVariant otherPackageVariant = (HolidayPackageVariant) obj;
            return Objects.equal(getTypeHolidayPackage(),otherPackageVariant.getTypeHolidayPackage())
                    && Objects.equal(getHolidayPackage(),
                            otherPackageVariant.getHolidayPackage());
        }
        return false;
    }
}

FlightHolidayPackageVariant:

public final class FlightHolidayPackageVariant extends HolidayPackageVariant{
    private Destination originCity;

    @Override
    public boolean equals(Object obj) {

        if(super.equals(obj)){
            return Objects.equal(getOriginCity(),
                    ((FlightHolidayPackageVariant)(obj)).getOriginCity());
        }
        return false;
    }
}

这将确保只有相同的 Type 变体彼此相等。