3

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

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

我将如何仅覆盖该方法而不创建扩展它的客户类equals()以便仅比较SSIDBSSIDcapabilties和属性?换句话说,我想在方法中消除属性,这样当我比较这两个对象时,方法会返回一个值:levelfrequencyequalstimestampequals()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当我尝试实现构造函数时出现以下错误:The constructor ScanResult() is not visible

4

4 回答 4

4

You just have to implement it without checking the fields you want to ignore. Don't forget to override the hashode() too.

@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
}
于 2013-04-08T18:21:34.647 回答
1

Extend the ScanResult class and only override the equals() method

class CustomScanResult extends ScanResult {

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

}
于 2013-04-08T18:17:20.673 回答
1

这是你重写的 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;
    }
}
于 2013-04-08T18:22:45.987 回答
0

简单的方法使用检查所有变量是否为真。例子:

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;
        }
    }
}
于 2019-08-24T18:28:16.877 回答