0

我有一个问题,我试图使用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超类的方法永远不会被调用。

超类生成的NetBeansequalshashCode方法如下:

@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比较中的字段。

4

1 回答 1

1

您没有声明Classifier_Excluded_DateTimeExcluded_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()调用的错误)。

于 2012-10-21T16:27:52.600 回答