Overriding “equals” method: how to figure out the type of the parameter?

前端 未结 9 1276
栀梦
栀梦 2021-02-05 07:47

I\'m trying to override equals method for a parameterized class.

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return t         


        
9条回答
  •  野性不改
    2021-02-05 08:16

    You can do it by retaining a reference to Class type. However, in my opinion, equality tests should be about the values the objects represent rather than the concrete types the values get expressed.

    A classic example of this is the Collections API for example. new ArrayList().equals(new LinkedList()) returns true. While these have completely different types, they represent the same value, namely "an empty collection".

    Personally, should two Tuples that represent the same data (e.g. ("a", "b")) be not equal, because one is of type Tuple while the other is Tuple?

    提交回复
    热议问题