equals
方法,该方法可以直接用于继承。相等性由两个对象决定。
belonging to subclasses of the base class. This is easily achievable using
if (!(anObject instanceof BaseClass)) return false;
having the same ID. The ID field is defined by the base class so we can here test that.
if (this.id != ((BaseClass) anObject).id) return false;
belonging to the same class. This is where I have the problem. Two objects may be of different types (and so be in different lists), but have the same ID. I have to be able to distinguish them. How can I do this?
Object.getClass().equals(this.getClass())
来实现这个目的(假设你指的是特定实体的类类型而不是类属性)。 - Luiggi Mendozathis.getClass()
吗? - Jeroen Vannevel