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

时间:2021-07-13 20:47:23

I am using hibernate and id... is used for persistence (which is why it is omitted in comparison). (Also, using google guava helper equals)

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

HolidayPackageVariant:

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:

FlightHolidayPackageVariant:

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

Should I completely override the equals() or should I be invoking super.equals(...) in some way ?

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

2 个解决方案

#1


3  

This is how you should implement data objects: no inheritance allowed, composition only.

这是您应该如何实现数据对象:不允许继承,仅限组合。

These are immutable objects, but you could want to modify them, so remove final where needed. Also you may remove final from the class definition because Hibernate doesn't support it, but in that case, you should document that these classes are not eligible for inheritance.

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

The main advantages are all the ones described in Effective Java plus, in this case, you don't have to manage inheritance through Hibernate which can sometimes be a real pain.

主要优点是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);
  }
}

#2


2  

Following the Secret of Equals:

遵循平等的秘密:

HolidayPackageVariant:

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:

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

This will ensure that only the same Type of variants are equal to each other.

这将确保只有相同类型的变体彼此相同。

#1


3  

This is how you should implement data objects: no inheritance allowed, composition only.

这是您应该如何实现数据对象:不允许继承,仅限组合。

These are immutable objects, but you could want to modify them, so remove final where needed. Also you may remove final from the class definition because Hibernate doesn't support it, but in that case, you should document that these classes are not eligible for inheritance.

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

The main advantages are all the ones described in Effective Java plus, in this case, you don't have to manage inheritance through Hibernate which can sometimes be a real pain.

主要优点是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);
  }
}

#2


2  

Following the Secret of Equals:

遵循平等的秘密:

HolidayPackageVariant:

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:

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

This will ensure that only the same Type of variants are equal to each other.

这将确保只有相同类型的变体彼此相同。