将对象转换为一对

时间:2015-01-16 20:01:02

标签: java object casting

我想让我的“平等”方法起作用,但遇到了麻烦。这应该很容易,但我是新手。我认为我必须将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))
                                                                  ^

4 个答案:

答案 0 :(得分:1)

这并不容易,这比你想象的还要多。

你的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。要避免NPE,如果fst或snd为null,请在调用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。

答案 1 :(得分:0)

主要问题是,在进行投射后,您将与原始otherObject进行比较,而Object仍然是aPair类的实例,而不是您左侧的变量赋值Pair,其类型为public boolean equals(Object otherObject) { Pair aPair = (Pair)otherOject; //--------> ??? if(aPair.fst.equals(this.fst) && aPair.snd.equals(this.snd)) { return true; } }

所以,这应该让你去:

Pair

虽然要小心。你在做什么叫做unchecked cast。您的编译器可能会警告您。您不知道传递给equals方法的对象是否真的可以强制转换为Pair - 它必须是Pair的实例或String的子类,以便是一个有效的行动。因此,例如,如果您将Integer或{{1}}对象传递给方法,则您的强制转换可能会在运行时失败。

<强> EIT

@ NathanHughes的more complete answer这个问题告诉你如何检查转换(使用instanceof关键字),所以我不会在这里重复。

我推荐Oracle java教程文档用于此类事情。检查this tutorial的类和子类 - 最后是关于如何强制转换并检查它是否有效。

答案 2 :(得分:0)

最好的事情

@Override
public boolean equals(Object otherObject) {
  if (otherObject instanceof Pair) {
    Pair otherPair = (Pair)otherObject;
    return otherPair.fst.equals(fst) && otherPair.snd.equals(snd)) 
  }
  return false;
}

答案 3 :(得分:0)

您的otherObjectObject的实例,而fst上没有Object。需要更改为aPair.fst.equals