重写:Override
重载:Overload
import java.util.Objects; public class A { public static void main(String[] args) { Object obj; String str; User u1 = new User(1, "red", "12345"); User u2 = new User(1, "yellow", "12345"); System.out.println(u1 == u2);//false 不是一个对象,因为地址肯定不同 System.out.println(u1.equals(u2));//true 因为equals重写,所以只通过id进行判断 String s1 = new String("java"); String s2 = new String("java"); System.out.println(s1==s2); //false, 两个字符串不是同一个对象 System.out.println(s1.equals(s2)); //true, 两个字符串内容相同 } } class User { int id; String name; String pw; //generate 生成构造器 //构造方法重写 public User(int id, String name, String pw) { this.id = id; this.name = name; this.pw = pw; } //重写equals //自动生成 @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; //强制转型成User return id == user.id; } @Override public int hashCode() { return Objects.hash(id); } }