Implementing the equals method in java

前端 未结 7 941
醉酒成梦
醉酒成梦 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:09

    Check this out:

    http://www.javapractices.com/topic/TopicAction.do?Id=17

    If that article is too much detail, then the short of it is: Your implementation is correct, but you should keep some other things in mind:

    1. You will also have to implement hashCode.

    2. equals will no longer commpare the object's identity. Doesn't sound like that's a problem for you.

    3. You could add the @Override annotation to your equals method.

提交回复
热议问题