Java中调用超类的equals方法
我遇到一个问题,我试图使用子类的对象测试超类的Java中调用超类的equals方法,java,netbeans,subclass,equals,superclass,Java,Netbeans,Subclass,Equals,Superclass,我遇到一个问题,我试图使用子类的对象测试超类的equals方法: 我的超类是: public class Excluded_DateTime implements Serializable { private Date fromDate; private Time fromTime; private Date toDate; private Time toTime; private Valid active; 子类的不同之处在于将标识符作为键: public class Clas
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\u Excluded\u DateTime
对象的相等性,而不使用字段classifierxcludeddatetimeno
但是我发现超类的equals
方法从未被调用
超类的equals
和hashCode
方法生成的NetBeans如下所示:
@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;
}
以及以下子类:
@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\u Excluded\u DateTime
的两个实例是否相同,但没有在比较中包含字段classifierExcludedDateTimeNo
。您没有将Classifier\u Excluded\u DateTime
声明为Excluded\u DateTime
的子类。要做到这一点,您需要说:
public class Classifier_Excluded_DateTime extends Excluded_DateTime {
现在,似乎更像是将Excluded\u DateTime
视为Classifier\u Excluded\u DateTime
的一种委托,因为在Classifier\u Excluded\u DateTime
中有一个成员实例变量,类型为Excluded\u DateTime
(你说的private排除了\u DateTime classifierxcludeddatetime;
)我不认为这是你的意图,因为你在文章中谈论的是子类和超类
(更新:删除了我认为没有显式的
super.equals()
调用的错误).这两个类之间没有任何继承关联。你有一个组合关联。你到底想做什么?哇,你是对的,很抱歉我错过了。我会修改的-谢谢。我真傻,错过了两个类之间的扩展
关系。谢谢!不像我错过了你对supe的呼叫那样愚蠢r、 等于(…)
,这是我在发布原始答案时认为缺少的内容:-)