JAVA [ 类对象比较 ]

来源:互联网 发布:游大大旅游网 java 编辑:程序博客网 时间:2024/05/18 01:50
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.

}
原创粉丝点击