public boolean equals(Object o){ if (this == o) { return true; } if(o instanceof Person){ Person p=(Person) o; if(p.name==this.name){ return true; } } return false; } public int hashCode(){ return this.name.hashCode(); }
Person pa=new Person(); pa.setName("12"); Person pb=new Person(); pb.setName("12"); if(pa.equals(pb)){ System.out.println("=="); }else{ System.out.println("!="); } System.out.print(pa.hashCode()+"-----"+pb.hashCode());目的是为了name相同就让 两个对象相同,而不是比较内存引用是否一致