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

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

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

问题

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

  1. public class Point {
  2. private int x;
  3. private int y;
  4. public Point(int x, int y) {
  5. this.x = x;
  6. this.y = y;
  7. }
  8. public boolean equals(Object o) {
  9. if (o instanceof Point) {
  10. Point c = (Point) o;
  11. if (!(c.x == this.x && c.y == this.y)) {
  12. return false;
  13. } else {
  14. return true;
  15. }
  16. }
  17. return false;
  18. }
  19. }
英文:

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

  1. public class Point {
  2. private int x;
  3. private int y;
  4. public Point(int x, int y) {
  5. this.x = x;
  6. this.y = y;
  7. }
  8. public boolean equals(Object o) {
  9. if (o instanceof Point) {
  10. Point c = (Point) o;
  11. if (!(c.x == this.x && c.y == this.y)) {
  12. return false;
  13. } else {
  14. return true;
  15. }
  16. }
  17. return false;
  18. }
  19. }

答案1

得分: 1

这可以简化为:

  1. @Override
  2. public boolean equals(Object o) {
  3. if (!(o instanceof Point)) {
  4. return false;
  5. }
  6. Point c = (Point) o;
  7. return x == c.x && y == c.y;
  8. }

解释

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

例如:

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

可以简化为:

  1. return someBooleanCondition();

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

  1. if (!(c.x == this.x && c.y == this.y)) {
  2. return false;
  3. } else {
  4. return true;
  5. }

可以简化为:

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

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

英文:

This can be simplified to:

  1. @Override
  2. public boolean equals(Object o) {
  3. if (!(o instanceof Point)) {
  4. return false;
  5. }
  6. Point c = (Point) o;
  7. return x == c.x && y == c.y;
  8. }

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:

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

can be simplified to

  1. return someBooleanCondition();

Applying this to your example:

  1. if (!(c.x == this.x && c.y == this.y)) {
  2. return false;
  3. } else {
  4. return true;
  5. }

becomes:

  1. 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

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

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

希望对您有帮助!

英文:

I believe the following is the simplest

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

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:

确定