本帖最后由 crazy_primitive 于 2013-7-18 13:14 编辑
- public class ReflectTest {
- public static void main(String[] args) {
- Collection collections = new HashSet();
- ReflectPoint rpt1 = new ReflectPoint(4,5);
- ReflectPoint rpt2 = new ReflectPoint(7,2);
- ReflectPoint rpt3 = new ReflectPoint(4,5);
- collections.add(rpt1);
- collections.add(rpt2);
- collections.add(rpt3);
- collections.add(rpt1);
- System.out.println(collections.size());
- }
- }
- public class ReflectPoint {
- private int x;
- public int y;
- public ReflectPoint(int x, int y) {
- super();
- this.x = x;
- this.y = y;
- }
- /* @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + x;
- result = prime * result + y;
- return result;
- }*/
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- ReflectPoint other = (ReflectPoint) obj;
- if (x != other.x)
- return false;
- if (y != other.y)
- return false;
- return true;
- }
- }
- //将hashCode()方法注释掉之前,System.out.println(collections.size())打印的是2
- //,这个好理解,可为什么注释掉之后打印的是3呢?
复制代码 |