处理继承时覆盖equals方法 [英] overriding equals method when dealing with inheritance

查看:34
本文介绍了处理继承时覆盖equals方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关在处理子类时如何最好地覆盖 equals 方法的内容,在这里我找到了很多帖子.他们推荐了使用 instanceof 或 getClass() 来比较不同子类的对象来实现解决方案的不同方法.

I have been reading about how best to override the equals method when dealing with subclasses and here I have found quite a few posts. They recommend different ways of implementing a solution using instanceof or getClass() to compare objects of different sub classes.

然而,关于 Effective Java,我的理解是(我是新手,所以我很可能是错的!)Bloch 认为最终两者都可能有问题,没有办法扩展可实例化的类,并且添加一个值组件,同时保留 equals 约定,除非您愿意放弃面向对象抽象的好处".然后建议优先组合而不是继承".

However with reference to Effective Java, my understanding is (and I am new to this so I may well be wrong!) Bloch argues that in the end both can be problematic, "There is no way to extend an instantiable class and add a value component while preserving the equals contract, unless you are willing to forgo the benefits of object-oriented abstraction". Then recommends to "favour composition over inheritance".

所以我正在处理这个类层次结构:AbstractClass、ConcreteClass1 和 ConcreteClass2.ConcreteClass1 扩展了 AbstractClass,ConcreteClass2 扩展了 ConcreteClass1.目前只有 AbstractClass 覆盖了 equals 方法.

So I am dealing with this class hierarchy: AbstractClass, ConcreteClass1 and ConcreteClass2. ConcreteClass1 extends AbstractClass and ConcreteClass2 extends ConcreteClass1. At the moment only AbstractClass overrides the equals method.

所以在抽象类中:

public abstract class AbstractClass {
        private String id;


        public boolean equals(Object other) {
            return other != null && other.getClass().equals(getClass())
                    && id.equals(((AbstractClass) other).id);
        }

    }

在 ConcreteClass1 中,我有:

And in ConcreteClass1 I have:

public class ConcreteClassOne extends AbstractClass
{
  private final AbstractClass parent;

  public ConcreteClassOne( String anId, AbstractClass aParent )
  {
    super( anId );

    parent = aParent;
  }

}

终于在 ConcreteClassTwo 中了:

Finally in ConcreteClassTwo I have:

public class ConcreteClassTwo extends ConcreteClassOne
{
  private static int nextTrackingNo = 0;

  private final int trackingNo;

  public ConcreteClassTwo ( String anId )
  {
    super( anId, null );

    trackingNo= getNextTrackingNo();
  }
}

所以我相信我需要覆盖 ConcreteClassOne 和 ConcreteClassTwo 中的 equals 方法以包含重要的字段 parent 和 trackingNo.我不允许更改设计,因此不能选择使用组合.有什么建议?

So I believe I need to override equals method in both ConcreteClassOne and ConcreteClassTwo to include the significant fields parent and trackingNo. I'm not allowed to change the design so using composition is not an option. Any suggestions?

推荐答案

最简单的方法是在具体类和抽象类中扩展 equals().

The simplest approach is to extend equals() in both the concrete and the abstract classes.

public class ConcreteClassTwo extends ConcreteClassOne {
    public boolean equals(Object other) {
        boolean rv = super.equals( other );
        if ( other instanceof ConcreteClassTwo ) {
           rv = rv && (this.trackingNo == ((ConcreteClassTwo) other).trackingNo);
        }
        return rv;
    }
}

这篇关于处理继承时覆盖equals方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆