Casting in equals method

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

    The declared type of obj is Object, so you must cast it to tell the compiler that it is a Dog.

    Although logically it can't be anything else at that point in the code, the compiler doesn't know anything about logic - it only knows about the type.

提交回复
热议问题