Casting in equals method

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

    0 讨论(0)
  • 2020-12-31 10:11

    Because as method parameter obj is Instance of Object class. So in the method body you need to cast obj back to the Dog type.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-31 10:15

    For JVM it is not that obvious that obj is Dog, so you have to be explicit with the cast.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-31 10:23

    Because you are defining your own parameters for equality, you have to make sure they are the same class. That is, unless you're comparing them the == way, then you need to compare some value inside the objects. To compare values inside the objects, they need to be the same type!

    For example, let's say you have two Dogs.

    Dog dog1 = new Dog("Fido");
    Dog dog2 = new Dog("Rover");
    

    If you want to test if they have the same name, as I'm sure you know, you can't use:

    if(dog1 == dog2)
    

    So you override the equals method. However, because you're overriding it, it has to have the same method signature. A method signature is defined by the name of the method, and the number and type of it's parameters. Which means if you wish to override it, it needs to have a parameter of type Object. Hence:

    if(dog1.equals(dog2))
    

    The reason you need to cast it to use whatever method you're using to get the name value from the dog, and compare those values.

    A note on your class design

    The convention in object oriented programming, and certainly in Java, is to have Accessor and Mutator methods to get and change variables in a class. That is:

    dog1.name; ----> dog1.getName();
    

    where getName() looks like:

    public String getName()
    {
        return name;
    }
    
    0 讨论(0)
提交回复
热议问题