在Java中调用equals超类的方法

时间:2012-10-21 16:21:36

标签: 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 Classifier_Excluded_DateTime implements Serializable {

private Integer classifierExcludedDateTimeNo;    
private Excluded_DateTime classifierExcludedDateTime;   

所以我想在不使用字段Classifier_Excluded_DateTime的情况下测试classifierExcludedDateTimeNo个对象的相等性。

但我发现的是,永远不会调用超类的equals方法。

NetBeans生成的超类的equalshashCode方法如下:

@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

1 个答案:

答案 0 :(得分:1)

您没有将Classifier_Excluded_DateTime声明为Excluded_DateTime的子类。要做到这一点,你需要说:

public class Classifier_Excluded_DateTime extends Excluded_DateTime {

现在,似乎更像是将Excluded_DateTime视为Classifier_Excluded_DateTime的一种委托,因为Classifier_Excluded_DateTime中的成员实例变量的类型为{{ 1}}(你说Excluded_DateTime的地方)。我不认为这是你的意图,因为你在谈论你的帖子中的子类和超类。

(更新:删除错误,我认为没有明确的private Excluded_DateTime classifierExcludedDateTime;电话)。