英文:
Why equals returns false in my java code?
问题
我不明白为什么在我的代码中 equals() 方法返回的是 "false" 而不是 "true"。
class Location {
private int x, y;
public Location(int x, int y) {
this.x = x;
this.y = y;
}
}
public class App {
public static void main(String[] args) {
Location a = new Location(1, 2); //
Location b = new Location(1, 2); // 相同的值
System.out.println(a.equals(b)); // 结果: "false"
}
}
如何比较两个对象的值?
英文:
I do not understand why equals() returns "false" instead of "true" in my code ?
class Location {
private int x, y;
public Location(int x, int y) {
this.x = x;
this.y = y;
}
}
public class App {
public static void main(String[] args) {
Location a = new Location(1, 2); //
Location b = new Location(1, 2); // same values
System.out.println(a.equals(b)); // result : "false"
}
}
How to compare the values of two objects ?
答案1
得分: 5
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Location that = (Location) o;
return x.equals(that.x) && y.equals(that.y);
}
英文:
Override the base 'equals' method with this:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Location that = (Location) o;
return x.equals(that.x) &&
y.equals(that.y);
}
</details>
# 答案2
**得分**: 0
这种方法在Object类中定义,以便每个Java对象都继承它。默认情况下,它的实现比较对象内存地址,因此它的工作方式与==运算符相同。然而,我们可以覆盖这个方法,以便为我们的对象定义相等的含义。
你应该覆盖此类的equals()方法,以便我们可以基于它们的内部细节来比较两个位置:
```java
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Location that = (Location) o;
return x.equals(that.x) && y.equals(that.y);
}
英文:
This method is defined in the Object class so that every Java object inherits it. By default, its implementation compares object memory addresses, so it works the same as the == operator. However, we can override this method in order to define what equality means for our objects.
You Should override override the equals() method for this class so that we can compare two Locations based on their internal details:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Location that = (Location) o;
return x.equals(that.x) &&
y.equals(that.y);
}
答案3
得分: 0
重写 equals()
方法就像是一个常规操作。你可以使用 IDE 工具来生成 equals()
和 hashcode()
方法。
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Location other = (Location) obj;
if (!getEnclosingInstance().equals(other.getEnclosingInstance()))
return false;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
英文:
Overriding equals()
method is like a routine. You can use IDE tools to generate equals()
and hashcode()
methods.
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Location other = (Location) obj;
if (!getEnclosingInstance().equals(other.getEnclosingInstance()))
return false;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论