对象的 equals 方法

发布于 2024-09-27 14:39:24 字数 379 浏览 1 评论 0原文

我正在尝试为对象编写一个 equals 方法来比较它们的字段,如果它们相等则返回 true。

private int x, y, direction;
private Color color;

public boolean equals(Ghost other){
   if (this.x == other.x && this.y == other.y &&
       this.direction == other.direction && this.color == other.color)
      return true;
   else 
      return false;
}

这可能有什么问题吗?

I'm trying to write an equals method for objects that compares their fields and return true if they're equal.

private int x, y, direction;
private Color color;

public boolean equals(Ghost other){
   if (this.x == other.x && this.y == other.y &&
       this.direction == other.direction && this.color == other.color)
      return true;
   else 
      return false;
}

What could be wrong with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

☆獨立☆ 2024-10-04 14:39:24

由于 color 似乎是 Color,这是一个类,因此是一个引用type,这意味着您需要使用equals()来比较颜色。

if (/* ... && */ this.color.equals(other.color)) {

正如注释中所指出的,使用 == 来比较引用类型实际上是在 Java 中比较内存地址。仅当它们都引用内存中的同一对象时,它才会返回 true


akf 指出您需要使用基本对象 类作为参数,否则您不会覆盖 Object.equals(),而是实际重载它,即提供调用同名方法的不同方式。如果您碰巧偶然传递了完全不同类的对象,则可能会发生意外行为(尽管如果它们属于不同类,无论如何它都会正确返回 false )。

@Override
public boolean equals(Object obj) {
    if (!(obj instanceof Ghost))
        return false;

    // Cast Object to Ghost so the comparison below will work
    Ghost other = (Ghost) obj;

    return this.x == other.x
        && this.y == other.y
        && this.direction == other.direction
        && this.color.equals(other.color);
}

Since color appears to be a Color, that's a class, and therefore a reference type, which means you need to use equals() to compare the colors.

if (/* ... && */ this.color.equals(other.color)) {

As noted in the comments, using == to compare reference types is really comparing memory addresses in Java. It'll only return true if they both refer to the same object in memory.


akf points out that you need to use the base Object class for your parameter, otherwise you're not overriding Object.equals(), but actually overloading it, i.e. providing a different way of calling the same-named method. If you happen to pass an object of a totally different class by accident, unexpected behavior might occur (although then again if they are of different classes it will return false correctly anyway).

@Override
public boolean equals(Object obj) {
    if (!(obj instanceof Ghost))
        return false;

    // Cast Object to Ghost so the comparison below will work
    Ghost other = (Ghost) obj;

    return this.x == other.x
        && this.y == other.y
        && this.direction == other.direction
        && this.color.equals(other.color);
}
瀞厅☆埖开 2024-10-04 14:39:24

原则上,这看起来不错。

但请注意,您正在使用 == 进行比较。对于基元来说,这没有问题,但对于对象来说,它将检查相同的实例,而不是相同的值。这可能是也可能不是您想要的。如果您要比较例如 java.lang.Strings,则需要使用equals(并检查null)。

In principle, this looks fine.

Note however that you are comparing using ==. For primitives, this is no problem, but for objects it will check for the same instance, not the same value. This may or may not be what you want. If you are comparing e.g. java.lang.Strings, you'd want to use equals instead (and check for null).

八巷 2024-10-04 14:39:24

如果您要比较对象变量而不是基本类型,则应该使用 this.color.equals(other.color) 比较。

就您而言,这还取决于您创建 Color 对象的方式。如果您使用静态实例(例如 Color.BLUE),那么实际上,这应该不重要。如果您从 RGB 值创建 Color 对象,那么它绝对很重要。无论哪种方式,最好习惯使用 .equals() 作为对象变量。

If you are comparing object variables instead of primitive types, you should be using a this.color.equals(other.color) comparison instead.

In your case, it also depends on how you created the Color objects. if you used the static instances (such as Color.BLUE), then actually, it shouldn't matter. If you created the Color object from rgb values, it definitely matters. Either way, it is best to get used to using .equals() for object variables.

眼波传意 2024-10-04 14:39:24

需要考虑的一件事是,当您更改参数类型时,您不会覆盖 Object 中的 equals 方法。您可能会发现此方法不会像您预期的那样在所有情况下都使用。而不是:

public boolean equals(Ghost other){

你应该:

public boolean equals(Object other){

然后在内部测试 other 参数是否是 instanceof Ghost 并根据需要进行强制转换。

One thing to consider is that you are not overriding the equals method from Object, as you are changing the param type. You might find this method will not be used in all cases as you might expect. Instead of:

public boolean equals(Ghost other){

you should have:

public boolean equals(Object other){

and then internally test whether the other param is an instanceof Ghost and cast as necessry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文