Implementing the equals method in java

前端 未结 7 1036
醉酒成梦
醉酒成梦 2021-01-18 15:39

This is my implementation of the equals class for a Coor class which is just contains 2 ints x and y. would this be the proper way of implementing this method?



        
7条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-18 16:12

    Here’s a more straightforward way:

    public boolean equals(Object other) {
        return other instanceof Coor
          && ((Coor) other).x == x
          && ((Coor) other).y == y
    }
    

提交回复
热议问题