|
|
@@ -1,4 +1,7 @@
|
|
|
-package exericse.exericse03;
|
|
|
+package exericse.exericse01;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* @author WanJl
|
|
|
@@ -8,15 +11,16 @@ package exericse.exericse03;
|
|
|
* @create 2026/7/29
|
|
|
*/
|
|
|
public class Student {
|
|
|
- // 属性
|
|
|
private String name; // 姓名
|
|
|
private Integer age; // 年龄(使用包装类)
|
|
|
private String studentId; // 学号
|
|
|
+ private ArrayList<String> courses; // 已选课程列表
|
|
|
|
|
|
/**
|
|
|
* 无参构造方法
|
|
|
*/
|
|
|
public Student() {
|
|
|
+ courses = new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -29,9 +33,11 @@ public class Student {
|
|
|
this.name = name;
|
|
|
this.age = age;
|
|
|
this.studentId = studentId;
|
|
|
+ courses = new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
- // ========== Getter 和 Setter 方法 ==========
|
|
|
+
|
|
|
+// ========== Getter 和 Setter 方法 ==========
|
|
|
|
|
|
public String getName() {
|
|
|
return name;
|
|
|
@@ -57,34 +63,33 @@ public class Student {
|
|
|
this.studentId = studentId;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 重写 toString 方法,方便打印对象信息
|
|
|
- */
|
|
|
- @Override
|
|
|
- public String toString() {
|
|
|
- return "Student{" +
|
|
|
- "name='" + name + '\'' +
|
|
|
- ", age=" + age +
|
|
|
- ", studentId='" + studentId + '\'' +
|
|
|
- '}';
|
|
|
+ public ArrayList<String> getCourses() {
|
|
|
+ return courses;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCourses(ArrayList<String> courses) {
|
|
|
+ this.courses = courses;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 重写 equals 方法,基于学号判断两个学生是否相等
|
|
|
- */
|
|
|
@Override
|
|
|
- public boolean equals(Object obj) {
|
|
|
- if (this == obj) return true;
|
|
|
- if (obj == null || getClass() != obj.getClass()) return false;
|
|
|
- Student student = (Student) obj;
|
|
|
- return studentId != null && studentId.equals(student.studentId);
|
|
|
+ public boolean equals(Object o) {
|
|
|
+ if (o == null || getClass() != o.getClass()) return false;
|
|
|
+ Student student = (Student) o;
|
|
|
+ return Objects.equals(name, student.name) && Objects.equals(age, student.age) && Objects.equals(studentId, student.studentId) && Objects.equals(courses, student.courses);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 重写 hashCode 方法,基于学号生成哈希值
|
|
|
- */
|
|
|
@Override
|
|
|
public int hashCode() {
|
|
|
- return studentId != null ? studentId.hashCode() : 0;
|
|
|
+ return Objects.hash(name, age, studentId, courses);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return "Student{" +
|
|
|
+ "name='" + name + '\'' +
|
|
|
+ ", age=" + age +
|
|
|
+ ", studentId='" + studentId + '\'' +
|
|
|
+ ", courses=" + courses +
|
|
|
+ '}';
|
|
|
}
|
|
|
}
|