62,628
社区成员
发帖
与我相关
我的任务
分享package test;
public class TestHashCode {
private int a;
public TestHashCode(int b){
this.a = b;
}
// @Override
// public int hashCode(){
// return a;
// }
@Override
public boolean equals(Object o){
return o instanceof TestHashCode && (a == ((TestHashCode)o).a);
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
public class Test {
public static void main(String[] args){
//测试hashCode
TestHashCode test1 = new TestHashCode(1);
TestHashCode test2 = new TestHashCode(1);
System.out.println(test1.equals(test2));
System.out.println(test1.hashCode());
System.out.println(test2.hashCode());
}
}

public class EntBlockGroup{
private int group_id;
private String group_name;
private int block_type_id;
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
return (obj instanceof EntBlockGroup)&&(group_id==((EntBlockGroup)obj).group_id)&&(block_type_id==((EntBlockGroup)obj).block_type_id);
}
}
public class Test {
private String s = null;
private int id = 0;
.
.
.
public int hashCode() {
int result = 17;
result = 37 * result + s.hashCode();
result = 37 * result + id;
return result;
}
}