在处理继承时重写equals方法 [英] overriding equals method when dealing with inheritance

查看:158
本文介绍了在处理继承时重写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认为最终两者都有问题,除非你愿意放弃,否则没有办法扩展可实例化的类并在保留等于合同的同时添加一个值组件。面向对象抽象的好处。然后建议赞成组合优于继承。

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.

所以在AbstractClass中:

So in AbstractClass:

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

所以我相信我需要在两者中重写equals方法ConcreteClassOne和ConcreteClassTwo包含有效字段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天全站免登陆