class Person
{
private String name;
private int age;
Person(String name,int age)
{
this.name=name;
this.age=age;
}
public int hashCode()//重写hashCode()自已调用
{
return name.hashCode()+age*39;//保证值的唯一性
}
public boolean equals(Object obj)//重写equals(Object obj)自已调用
{
if(!(obj instanceof Person))
return false;
Person p=(Person)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
} |