重写equals方法
public class Father { String name; public Father(String name) { this.name = name; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof Father)) { return false; } Father f = (Father) obj; return (this.name.equals(f.name)); } @Override public String toString() { return "Father [name=" + name + "]"; } } public class Son { public static void main(String[] args) { Father f1 = new Father("张飞"); Father f2 = new Father("张飞"); System.out.println(f1); System.out.println(f2); System.out.println(f1.equals(f2)); } } Father [name=张飞] Father [name=张飞] true