请前辈们讲解下如下代码中// 重写equals方法部分代码的执行思路
其中的equals(Object obj) { if (this == obj) {........中定义的obj和this指的是什么,
麻烦将stu3对象代入讲解一下...........大二java初学者--谢谢--谢谢
import java.util.*;
class Student {
private String id;
private String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
// 重写toString()方法
public String toString() {
return id + ":" + name;
}
// 重写hashCode方法
public int hashCode() {
return id.hashCode();
}
// 重写equals方法
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Student)) {
return false;
}
Student stu = (Student) obj;
boolean b = this.id.equals(stu.id);
return b;
}
}
public class Example11 {
public static void main(String[] args) {
HashSet hs = new HashSet();
Student stu1 = new Student("1", "Jack");
Student stu2 = new Student("2", "Rose");
Student stu3 = new Student("2", "Rose");
hs.add(stu1);
hs.add(stu2);
hs.add(stu3);
System.out.println(hs);
}
}