public class Person{
private static long counter = 0;
private long id = counter++;//这里错了,不能用final修饰
public long getId(){
return id;
}
public String toString(){
return super.toString()+" "+id;
}
public boolean equals(Object obj){
if(obj instanceof Person){
Person p = (Person)obj;//这里错了,这个叫做类型转换异常,不能将人转换成人
if(this.getId() == p.getId()){
return true;
}
}
return super.equals(obj);
}
public int hashCode(){
return String.valueOf(id).hashCode();
}
} |