Casting in equals method

前端 未结 7 1401
攒了一身酷
攒了一身酷 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:04

    You are overriding equals method to check equality by value not by reference..

    i.e You want two dogs equal if they have same name and age not if they are owned by the same person(i.e not if their references are equal)


    Without cast you can't access dog's name and age members

提交回复
热议问题