Is there a way to simplify this code comparing two Points with fields x and y for equality?

huangapple go评论62阅读模式
英文:

Is there a way to simplify this code comparing two Points with fields x and y for equality?

问题

下面的代码旨在比较具有x和y字段的两个Point对象是否相等。有没有简化这段代码的方法?

public class Point {
    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public boolean equals(Object o) {
        if (o instanceof Point) {
            Point c = (Point) o;
            if (!(c.x == this.x && c.y == this.y)) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
}
英文:

Is there a way to simplify the following code which aims to compare two Points with fields x and y for equality?

public class Point {
    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public boolean equals(Object o) {
        if (o instanceof Point) {
            Point c = (Point) o;
            if (!(c.x == this.x && c.y == this.y)) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

}

答案1

得分: 1

这可以简化为:

@Override
public boolean equals(Object o) {
    if (!(o instanceof Point)) {
        return false;
    }
    Point c = (Point) o;
    return x == c.x && y == c.y;
}

解释

将条件中返回true/false的if/then布尔条件替换为布尔本身的结果(或取反的结果)通常会使代码变得更短、更清晰。

例如:

if (someBooleanCondition()) { return true; } else { return false; }

可以简化为:

return someBooleanCondition();

将这个思想应用到你的例子中:

if (!(c.x == this.x && c.y == this.y)) {
    return false;
} else {
    return true;
}

可以简化为:

return c.x == this.x && c.y == this.y;

我将instanceof检查放在前面,因为我认为在异常情况下使用短路返回会使代码更易读,不过这是主观的。

英文:

This can be simplified to:

@Override
public boolean equals(Object o) {
    if (!(o instanceof Point)) {
        return false;
    }
    Point c = (Point) o;
    return x == c.x && y == c.y;
}

Explanation

It is shorter and often clearer to replace an if/then boolean conditional that returns true/false in the condition with the results (or negated results) of the boolean itself.

For instance:

if (someBooleanCondition()) { return true; } else { return false; }

can be simplified to

return someBooleanCondition();

Applying this to your example:

if (!(c.x == this.x && c.y == this.y)) {
    return false;
} else {
    return true;
}

becomes:

return c.x == this.x && c.y == this.y;

I've put the instanceof check first, since I feel short-circuit returning the exceptional case leads to easier to read code, though that is subjective.

答案2

得分: 0

以下是最简单的代码示例:

public boolean equals(Object o) {
    if (o == null || o.getClass() == getClass()) return false;
    return x == ((Point) o).x && y == ((Point) o).y;
}

希望对您有帮助!

英文:

I believe the following is the simplest

public boolean equals(Object o) {
    if (o == null || o.getClass() == getClass()) return false;
    return x == ((Point) o).x && y == ((Point) o).y;
}

Hope it helped you!

huangapple
  • 本文由 发表于 2020年8月27日 11:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63608733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定