您的位置:首页 > 编程语言 > Java开发

JAVA [ 类对象比较 ]

2011-07-27 18:59 309 查看
public class Customer{

private String name;
private int age;

public Customer(String name,int age){
this.name = name;
this.age = age;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

public boolean equals(Object o){
if(this == o) return true;
if(!(o instanceof Customer)) return false;
final Customer other = (Customer)o;
if(this.name.equals(other.getName())&& this.age == other.getAge())
return true;
else
return false;
}

public int hashCode(){
int result;
result = (name == null?0:name.hashCode());
result = 29 * result + age;
return result;
}

/*
* collection.Customer@03 collection.Customer@04
* hashCode |01| |02| |03| |04| ... |09|
* Object o3 o5 o2
* o1 o4 o6(o7)[通过equals判别]
*/

//if a.equals(b), then a and b must have the same hash code.

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: