|
测试代码:
- /*
- * Created on 2004-7-26
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- /**
- * @author yiningchen
- *
- * TODO To change the template for this generated type comment go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- public class HashTest {
-
- private int id;
- private String name;
-
- public HashTest(int id, String name)
- {
- this.id = id;
- this.name = name;
- }
- public boolean equals(Object obj)
- {
- if (obj instanceof HashTest)
- {
- return (id==((HashTest)obj).id);
- }
- else
- {
- return false;
- }
- }
-
- public int hashCode()
- {
- return (id + name.hashCode());
- }
-
- public static void main(String[] args)
- {
- HashTest obj1 = new HashTest(1, "abc");
- HashTest obj2 = new HashTest(1, "def");
-
- System.out.println("obj1==obj2: " + obj1.equals(obj2));
- System.out.println("obj1 hash: " + obj1.hashCode());
- System.out.println("obj2 hash: " + obj2.hashCode());
- }
- }
复制代码 |
|