Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 列表中的contains()方法未按预期工作_Java_Collections_Java Collections Api - Fatal编程技术网

Java 列表中的contains()方法未按预期工作

Java 列表中的contains()方法未按预期工作,java,collections,java-collections-api,Java,Collections,Java Collections Api,contains()的api方法说 如果此列表包含指定的元素,则返回true。更正式地说,当且仅当此列表包含至少一个元素e时,才返回true,即(o==null?e==null:o.equals(e))。 " 我在类中重写了equals()方法,但是contains()检查时仍然返回false 我的代码 class Animal implements Comparable<Animal>{ int legs; Animal(int legs){this.legs=le

contains()的api
方法说

如果此列表包含指定的元素,则返回true。更正式地说,当且仅当此列表包含至少一个元素e时,才返回true,即(o==null?e==null:o.equals(e))。 "

我在类中重写了
equals()
方法,但是
contains()
检查时仍然返回false

我的代码

class Animal implements Comparable<Animal>{
    int legs;
    Animal(int legs){this.legs=legs;}
    public int compareTo(Animal otherAnimal){
        return this.legs-otherAnimal.legs;
    }
    public String toString(){return this.getClass().getName();}

    public boolean equals(Animal otherAnimal){
        return (this.legs==otherAnimal.legs) && 
                (this.getClass().getName().equals(otherAnimal.getClass().getName()));
    }

    public int hashCode(){
        byte[] byteVal = this.getClass().getName().getBytes();
        int sum=0;
        for(int i=0, n=byteVal.length; i<n ; i++)
            sum+=byteVal[i];
        sum+=this.legs;
        return sum;
    }

}
class Spider extends Animal{
    Spider(int legs){super(legs);}
}
class Dog extends Animal{
    Dog(int legs){super(legs);}
}
class Man extends Animal{
    Man(int legs){super(legs);}
}

您重载了
等于
,而不是重写它。要重写
对象
等于
方法,必须使用相同的签名,这意味着参数必须是
对象
类型

改为:

@Override
public boolean equals(Object other){
    if (!(other instanceof Animal))
        return false;
    Animal otherAnimal = (Animal) other;
    return (this.legs==otherAnimal.legs) && 
           (this.getClass().getName().equals(otherAnimal.getClass().getName()));
}
作为

类C中声明的实例方法m1重写另一个实例 方法m2,在类A中声明,如果以下所有条件均为真:


签名必须与覆盖签名相同,在您的情况下,该签名将被忽略

爬行器有8条腿:)请注意,当您打算重写函数时,使用
@Override
注释将有助于防止类似的错误。
@Override
public boolean equals(Object other){
    if (!(other instanceof Animal))
        return false;
    Animal otherAnimal = (Animal) other;
    return (this.legs==otherAnimal.legs) && 
           (this.getClass().getName().equals(otherAnimal.getClass().getName()));
}
C is a subclass of A.

The signature of m1 is a subsignature  of the signature of m2.

Either:

m2 is public, protected, or declared with default access in the same package as C, or

m1 overrides a method m3 (m3 distinct from m1, m3 distinct from m2), such that m3 overrides m2.