Casting in equals method

前端 未结 7 1399
攒了一身酷
攒了一身酷 2020-12-31 09:46

I have a question about overriding the equals method in Java. In my book, I have the following example:

public class Dog{
     private String na         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 10:15

    I think that your demo class is missing a hashCode() method. The two objects can only be equal if their call to an overridden hashCode() is equal. (the same integer value).

    I'm pretty sure you need both to guarantee this equation.

提交回复
热议问题