} class MyTest { int x; int y; public MyTest(int x,int y) { 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; MyTest other = (MyTest) obj; if (x != other.x) return false; if (y != other.y) return false; return true; } } 4、package me.test; import java.util.*; public class ReflectTest2 { public static void main(String[]args) { //面向父类的编程或者面向接口编程 Collection c1=new HashSet() ; MyTest t1=new MyTest(2,2) ; MyTest t2=new MyTest(3,4) ; MyTest t3=new MyTest(2,2) ; c1.add(t1) ; c1.add(t2) ; c1.add(t3) ; c1.add(t1) ; t1.y=0; c1.remove(t1); System.out.println(c1.size()); }
} class MyTest { int x; int y; public MyTest(int x,int y) { 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; MyTest other = (MyTest) obj; if (x != other.x) return false; if (y != other.y) return false; return true; } }