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.
}
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.
}
相关文章推荐
- Java几种创建对象速度的比较
- JAVA对象比较器 Comparator
- Java中Comparable和Comparator实现对象比较
- java比较接口Comparable,对象排序存在TreeSet中
- Java对象比较、空判断等工具
- java中Comparable实现对象的比较
- java实现面向对象和javaScript基于对象的区别&java垃圾回收机制和其他编程语言的比较
- Java中对象的比较 == 和 equals()
- java 对象的比较操作
- Java对象之间的比较之equals和==
- Java从入门到精通Chapter7_3对象比较
- JAVA中自定义对象比较时重写equals…
- Java中Comparable和Comparator实现对象比较
- java中对象的比较,“==”与equal()区分的例子
- java对象比较之Comparable和Comparator
- 【Java基础之集合(三)】集合对象比较大小和比较重复的区别和不同实现
- (二十)Java工具类Diffable、DiffBuilder、Diff比较对象的区别及建造者模式详解
- (二十)Java工具类Diffable、DiffBuilder、Diff比较对象的区别及建造者模式详解
- 增加java基础——hashSet中存放对象的比较规则
- Java中Comparable和Comparator实现对象比较