gpt4 book ai didi

java - 将对象类型转换成一对

转载 作者:行者123 更新时间:2023-11-29 02:57:51 24 4
gpt4 key购买 nike

我正在尝试让我的“等于”方法起作用,但遇到了问题。这应该很容易,但我对此并不陌生。我以为我必须将 otherOject 转换为一对才能使用 .fst 并检查这对是否相等,但是我很难正确地“转换”。任何帮助将非常感激。我有以下方法:

public void setFst(T1 aFirst)
{
fst = aFirst;
}

public void setSnd(T2 aSecond)
{
snd = aSecond;
}

public boolean equals(Object otherObject)
{
Pair aPair = (Pair)otherOject; //--------> ???

if(otherObject.fst.equals(this.fst) && otherObject.snd.equals(this.snd))
{
return true;
}
}

这是我遇到的错误:

./Pair.java:84: cannot find symbol
symbol : variable fst
location: class java.lang.Object
if(otherObject.fst.equals(this.fst) && otherObject.snd.equals(this.snd))
^
./Pair.java:84: cannot find symbol
symbol : variable snd
location: class java.lang.Object
if(otherObject.fst.equals(this.fst) && otherObject.snd.equals(this.snd))
^

最佳答案

这没那么容易,它的陷阱比你想象的要多。

您的 equals 方法必须允许对象属于您为其编写的类以外的类。你要做的就是检查参数是否也是一个对:

if (otherObject == null) return false;
if (otherObject.getClass() != Pair.class) return false;

通过此检查后,您可以安全地转换,并将转换对象分配给新的局部变量:

Pair otherPair = (Pair)otherObject;

然后使用 otherPair 上的字段进行相等检查。至此,您已完成 otherObject 参数的使用,equals 方法的其余部分不应再引用它。

整个事情看起来像

public boolean equals(Object otherObject) {
if (otherObject == null) return false;
if (getClass() != otherObject.getClass()) return false;
Pair otherPair = (Pair)otherObject;
return otherPair.fst.equals(this.fst) && otherPair.snd.equals(this.snd);
}

假设 fst 和 snd 不允许为 null。在 null 成员上调用 equals 方法将导致 NullPointerException。如果 fst 或 snd 为 null,为避免 NPE,请在调用 equals 之前检查成员是否为 null:

public boolean equals(Object otherObject) {
// check if references are the same
if (this == otherObject) return true;
// check if arg is null or something other than a Pair
if (otherObject == null) return false;
if (getClass != otherObject.getClass()) return false;
Pair otherPair = (Pair)otherObject;
// check if one object's fst is null and the other is nonnull
if (otherPair.fst == null || this.fst == null) {
if (otherPair.fst != null || this.fst != null) return false;
}
// check if one object's snd is null and the other is nonnull
if (otherPair.snd == null || this.snd == null) {
if (otherPair.snd != null || this.snd != null) return false;
}
// each member is either null for both or nonnull for both
return ((otherPair.fst == null && this.fst == null) || otherPair.fst.equals(this.fst))
&& ((otherPair.snd == null && this.snd == null) || otherPair.snd.equals(this.snd));
}

最后一点写起来很烦人,IDE 会为您生成这些东西。这是 Eclipse 生成的内容:

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (fst == null) {
if (other.fst != null)
return false;
} else if (!fst.equals(other.fst))
return false;
if (snd == null) {
if (other.snd != null)
return false;
} else if (!snd.equals(other.snd))
return false;
return true;
}

记得也要实现 hashCode。

关于java - 将对象类型转换成一对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27991889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com