contains()
方法的api说
"如果此列表包含指定的元素,则返回true。更正式地,当且仅当此列表包含至少一个元素e时才返回true(o == null?e == null:o.equals(e))。 "
我覆盖了班级中的equals()
方法,但是当我检查时,contains()
仍然返回我的错误
我的代码
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);}
}
原谅课程背后的坏概念,但我只是测试对我的概念的理解。
现在当我尝试这个时,它会打印false
,即使equals被覆盖
List<Animal> li=new ArrayList<Animal>();
Animal a1=new Dog(4);
li.add(a1);
li.add(new Man(2));
li.add(new Spider(6));
List<Animal> li2=new ArrayList<Animal>();
Collections.addAll(li2,new Dog(4),new Man(2),new Spider(6));
System.out.println(li2.size());
System.out.println(li.contains(li2.get(0))); //should return true but returns false
答案 0 :(得分:4)
您重载equals
而不是覆盖它。要覆盖Object
的{{1}}方法,您必须使用相同的签名,这意味着参数必须为equals
类型。
更改为:
Object
答案 1 :(得分:1)
在类C中声明的实例方法m1会覆盖另一个实例 方法m2,在A类中声明,如果满足以下所有条件:
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.
签名必须相同才能覆盖在您的情况下被忽略的情况!!!