[英]Calling equals method of superclass in Java
I have an issue where I am trying to use test the equals
method of a superclass using objects of a subclass: 我有一个问题,我试图使用子类的对象来测试超类的
equals
方法:
My superclass is: 我的超类是:
public class Excluded_DateTime implements Serializable {
private Date fromDate;
private Time fromTime;
private Date toDate;
private Time toTime;
private Valid active;
And the subclass differs by having an identifier as a key: 子类的不同之处在于将标识符作为关键字:
public class Classifier_Excluded_DateTime implements Serializable {
private Integer classifierExcludedDateTimeNo;
private Excluded_DateTime classifierExcludedDateTime;
So I want to test the equality of Classifier_Excluded_DateTime
objects without using the field classifierExcludedDateTimeNo
. 所以我想测试
Classifier_Excluded_DateTime
对象的相等性,而不使用字段classifierExcludedDateTimeNo
。
But what I am finding is that the equals
method of the superclass is never called. 但是我发现的是,从未调用过超类的
equals
方法。
The NetBeans generated equals
and hashCode
methods of the superclass are as follows: NetBeans生成的
equals
和hashCode
方法的超类如下:
@Override
public int hashCode() {
int hash = 7;
hash = 23 * hash + (this.fromDate != null ? this.fromDate.hashCode() : 0);
hash = 23 * hash + (this.fromTime != null ? this.fromTime.hashCode() : 0);
hash = 23 * hash + (this.toDate != null ? this.toDate.hashCode() : 0);
hash = 23 * hash + (this.toTime != null ? this.toTime.hashCode() : 0);
hash = 23 * hash + (this.active != null ? this.active.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Excluded_DateTime other = (Excluded_DateTime) obj;
if (this.fromDate != other.fromDate && (this.fromDate == null || !this.fromDate.equals(other.fromDate))) {
return false;
}
if (this.fromTime != other.fromTime && (this.fromTime == null || !this.fromTime.equals(other.fromTime))) {
return false;
}
if (this.toDate != other.toDate && (this.toDate == null || !this.toDate.equals(other.toDate))) {
return false;
}
if (this.toTime != other.toTime && (this.toTime == null || !this.toTime.equals(other.toTime))) {
return false;
}
if (this.active != other.active) {
return false;
}
return true;
}
And those of the subclass as follows: 以及那些子类如下:
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + (this.getClassifierExcludedDateTimeNo() != null ? this.getClassifierExcludedDateTimeNo().hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
if (! super.equals(obj)) {
return false;
}
final Classifier_Excluded_DateTime other = (Classifier_Excluded_DateTime) obj;
if (this.getClassifierExcludedDateTimeNo() != other.getClassifierExcludedDateTimeNo() && (this.getClassifierExcludedDateTimeNo() == null || !this.classifierExcludedDateTimeNo.equals(other.ClassifierExcludedDateTimeNo))) {
return false;
}
return true;
}
Can I do what I'm trying to do by amending these methods of either class? 我可以通过修改任一类的这些方法来做我想做的事情吗?
I am trying to test whether two instance of Classifier_Excluded_DateTime
are the same without including the field classifierExcludedDateTimeNo
in the comparison. 我正在尝试测试
Classifier_Excluded_DateTime
两个实例是否相同,而在比较中不包括字段classifierExcludedDateTimeNo
。
You are not declaring Classifier_Excluded_DateTime
as a subclass of Excluded_DateTime
. 您没有将
Classifier_Excluded_DateTime
声明为Excluded_DateTime
的子类。 To do that, you would need to say: 为此,您需要说:
public class Classifier_Excluded_DateTime extends Excluded_DateTime {
Right now, it seems more like you are treating Excluded_DateTime
as a kind of delegate of Classifier_Excluded_DateTime
, as you have a member instance variable inside of Classifier_Excluded_DateTime
that is of type Excluded_DateTime
(the place where you say private Excluded_DateTime classifierExcludedDateTime;
). 现在,您似乎更喜欢将
Excluded_DateTime
视为Classifier_Excluded_DateTime
的一种委托,因为您在Classifier_Excluded_DateTime
内部有一个成员实例变量,其类型为Excluded_DateTime
(在这里您说private Excluded_DateTime classifierExcludedDateTime;
)。 I don't think that's your intent, since you are talking about subclasses and superclasses in your post. 我不认为这是您的意图,因为您正在谈论帖子中的子类和超类。
(Update: removed error where I thought there was not an explicit super.equals()
call). (更新:删除了我认为没有明确的
super.equals()
调用的错误)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.