2015-01-16 32 views
1

我试图让我的“等于”方法工作,但遇到了麻烦。这应该很简单,但我对此很陌生。我想我必须将其他项目投给一对,以便能够使用.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)) 
                   ^
+2

你为什么使用'otherObject'而不是'aPair'? –

回答

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; 
    } 
} 

虽然要小心。你在做什么被称为未经检查的铸造。你的编译器可能会警告你。您不知道传递给您的equals方法的对象是否真的可以投射到Pair--它必须是Pair的实例或Pair的子类,因为这是一个有效的操作。因此,例如,如果您将StringInteger对象传递到方法中,则您的演员阵列在运行时可能会失败。

EIT

@ NathanHughes的more complete answer这个问题向您介绍如何检查铸造(使用的instanceof关键字),所以我就不在这里重复了。

我推荐Oracle java这个教程文档。检查this tutorial的类和子类 - 最后是关于如何投射并检查它是否有效投射。

+0

你是完全正确的,我传递字符串和整数,并得到一个运行时错误。任何想法,我会如何避免这种情况?这些指令是“如果该对等于aPair,则返回true”。我如何让字符串和整数之间进行比较,如果我正在执行未经检查的投射,有没有办法“检查”我的投射?我为我的java无能表示抱歉。 –

+0

每个人都必须学习。 @NathanHughes刚才的答案显示了如何检查铸件(使用instanceof关键字)。我推荐Oracle Java教程文档用于这种事情。查看http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html的类和子类 - 最后是关于如何投射并检查它是否有效投射 –

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不允许为空。在空成员上调用equals方法将导致NullPointerException。为了避免NPE如果FST或SND为空,检查成员对他们的呼吁平等之前是空的:

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)); 
} 

这最后一位是讨厌写,集成开发环境会产生这个东西给你。以下是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。

+0

谢谢!出于某种原因,当两个字符串具有相同字符但顺序不同时,此代码返回“true”。我将如何解决这个问题?对于我缺乏Java知识,我再次抱歉。 –

+0

@Ronon:这真的很奇怪。你确定它是这个方法还是你没有实现hashCode的问题?如果T1或T2在等号方法上有问题,你会在这里看到一个问题。 –

+0

测试(ZUeEuHVWxXWi,12)是否等于(ZUeEhHVWxXWi,12):PASS 测试(ZUeEhHVWxXWi,12)是否等于(UeEuHVWxXWi,12):FAIL(这是我运行测试程序时显示的内容,字符串是不相等的,但它返回的是,我相信这可能是我的代码的另一部分的问题。谢谢你的帮助! –

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; 
} 
+0

请在你的'if'和左括号之间加一个空格,好像人们已经变得如此担心浪费空间,他们试图通过使所有'if '''do ... until','while'块看起来像函数。疯狂! –

+0

倾向于将大括号放在与if语句的条件部分相同的行上,它可以让你获得更多的代码屏幕上,并且它减少了某人(通常意味着我们自己)在这两者之间作出陈述的机会,使整个块无条件地生效。 –

+0

不幸的是,这会返回一个空指针异常。我相信这是一个有效的解决方案,但是在我的代码中必然存在一个错误... –

0

otherObjectObject一个实例,并没有对Object没有fst。需要更改为aPair.fst.equals

+1

我想你的意思是说对象上没有'fst',因为'fst'只在'aPair'上# –

+0

@EdwinBuck是的一个错字,谢谢。 – fastcodejava

+1

不是问题,拼写错误会一直发生。 –