仅覆盖Java对象的equals()方法

时间:2013-04-08 18:11:36

标签: java android override equals

我正在开发一个使用ScanResult对象的Android应用程序。该对象采用以下形式:

[SSID: __mynetwork__, BSSID: 00:0e:2e:ae:4e:85, capabilities: [WPA-PSK-TKIP][ESS], level: -69, frequency: 2457, timestamp: 117455824743]

如何在不创建扩展它的客户类的情况下仅覆盖equals()方法,以便仅比较SSIDBSSIDcapabilties,{{1仅}}和level属性?换句话说,在frequency方法中我想要消除equals属性,这样当我比较这两个对象时,timestamp方法会返回equals()值:

true

注意:当我派生一个扩展[SSID: __mynetwork__, BSSID: 00:0e:2e:ae:4e:85, capabilities: [WPA-PSK-TKIP][ESS], level: -69, frequency: 2457, timestamp: 117455824743] [SSID: __mynetwork__, BSSID: 00:0e:2e:ae:4e:85, capabilities: [WPA-PSK-TKIP][ESS], level: -69, frequency: 2457, timestamp: 117460312231] 的客户类时,当我尝试实现构造函数时出现以下错误:ScanResult

4 个答案:

答案 0 :(得分:4)

您只需要在不检查要忽略的字段的情况下实现它。不要忘记覆盖hashode()。

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((field1 == null) ? 0 : field1.hashCode());
    result = prime * result + ((field2 == null) ? 0 : field2.hashCode());
            ...etc
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    ScanResult other = (ScanResult ) obj;
    if (field1 == null) {
        if (other.field1 != null)
            return false;
    } else if (!field1.equals(other.field1))
        return false;
    if (field2 == null) {
        if (other.field2 != null)
            return false;
    } else if (!field2 .equals(other.field2 ))
        return false;
        }
... etc
}

答案 1 :(得分:1)

扩展ScanResult类,仅覆盖equals()方法

class CustomScanResult extends ScanResult {

    @Override
    public boolean equals(Object o) {
        // ... custom logic ...
    }

}

答案 2 :(得分:1)

这是你的Overriden equals()方法......

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof ScanResult)) return false;

    if(!BSSID.equals(o.BSSID)) return false;
    if(!SSID.equals(o.SSID)) return false;
    if(!capabilities.equals(o.capabilities)) return false;
    if(frequency != o.frequency) return false;
    if(level != o.level) return false;

    return true;
}

修改

既然上述解决方案无效,我可以建议:

class ScanResultComparator {
    public static boolean equals(ScanResult a, ScanResult b){
        if(!a.BSSID.equals(b.BSSID)) return false;
        if(!a.SSID.equals(b.SSID)) return false;
        if(!a.capabilities.equals(b.capabilities)) return false;
        if(a.frequency != b.frequency) return false;
        if(a.level != b.level) return false;

        return true;
    }
}

答案 3 :(得分:0)

使用简单方法检查所有变量是否为true。 例如:

public class FtpFile {

    public String host;
    public String port;
    public String fileName;
    public String path;
    public String username;
    public String password;

    /**
     * Override Equals()
     */
    @Override
    public boolean equals(Object o) {
        try {
            if (o == null || getClass() != o.getClass())
                return false;

            FtpFile p = (FtpFile) o;
            return ((host.equals(p.host))
                    && (port.equals(p.port))
                    && (path.equals(p.path))
                    && (username.equals(p.username))
                    && (password.equals(p.password)));
        } catch (Exception ex) {
            return false;
        }
    }
}