[英]Calling equals method of superclass in Java
我有一个问题,我试图使用子类的对象来测试超类的equals
方法:
我的超类是:
public class Excluded_DateTime implements Serializable {
private Date fromDate;
private Time fromTime;
private Date toDate;
private Time toTime;
private Valid active;
子类的不同之处在于将标识符作为关键字:
public class Classifier_Excluded_DateTime implements Serializable {
private Integer classifierExcludedDateTimeNo;
private Excluded_DateTime classifierExcludedDateTime;
所以我想测试Classifier_Excluded_DateTime
对象的相等性,而不使用字段classifierExcludedDateTimeNo
。
但是我发现的是,从未调用过超类的equals
方法。
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;
}
以及那些子类如下:
@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;
}
我可以通过修改任一类的这些方法来做我想做的事情吗?
我正在尝试测试Classifier_Excluded_DateTime
两个实例是否相同,而在比较中不包括字段classifierExcludedDateTimeNo
。
您没有将Classifier_Excluded_DateTime
声明为Excluded_DateTime
的子类。 为此,您需要说:
public class Classifier_Excluded_DateTime extends Excluded_DateTime {
现在,您似乎更喜欢将Excluded_DateTime
视为Classifier_Excluded_DateTime
的一种委托,因为您在Classifier_Excluded_DateTime
内部有一个成员实例变量,其类型为Excluded_DateTime
(在这里您说private Excluded_DateTime classifierExcludedDateTime;
)。 我不认为这是您的意图,因为您正在谈论帖子中的子类和超类。
(更新:删除了我认为没有明确的super.equals()
调用的错误)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.