我正在尝试覆盖Java中的equals方法。我有一节课People
它基本上有2个数据字段name
和age
。现在我想覆盖equals
方法,这样我就可以在两个People对象之间进行检查。
我的代码如下
public boolean equals(People other){
boolean result;
if((other == null) || (getClass() != other.getClass())){
result = false;
} // end if
else{
People otherPeople = (People)other;
result = name.equals(other.name) && age.equals(other.age);
} // end else
return result;
} // end equals
但当我写下age.equals(other.age)
它给了我错误,因为equals方法只能比较字符串和年龄是整数。
解决方案
我用过==
运算符,我的问题就解决了。
https://stackoverflow.com/questions/8180430
复制相似问题