import java.util.*;
class Name{
private String firstname,secondname;
public Name(String firstname,String secondname){
this.firstname=firstname;
this.secondname=secondname;
}
public String getFirstName(){return firstname;}
public String getsecondName(){ return secondname; }
public String toString(){ return firstname+" "+secondname;}
public boolean equals(Object obj) {
if(obj instanceof Name){
return true;
} return super.equals(obj);
}
public int hashcode(){
return firstname.hashCode();
}
}
public class TestCollection {
public static void main(String[] args) {
Collection c=new HashSet();
c.add("hello");
c.add(new Name("uzi","omg"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("uzi","omg")));
System.out.println(c);
}
}
程序结果如下//
false
[uzi omg]
重写了Name 中的equals()结果依然没变 为何没有调用Name里的equals??