以下是我感到困惑的 4 种方法,这 4 种方法在教师类中。我有一个学生班和教师班。在 Teacher 类中,声明的是ArrayList<Student> students
as 实例变量。
如何解释我在下面给出的方法中看到的学生,它也用作参数。我对学生searchStudent
(在方法中)和Student student
(在论点中)感到非常困惑。难道只是为了ArrayList
?如何理解一个类将使用类名搜索另一个类的概念?
public Student searchStudent(Student student)
{
//confuses me
Student found = null;
if (this.students.contains(student))
{
int index = this.students.indexOf(student);
if (index != -1)
{
found = this.students.get(index);
}
}
return found;
}
public Student searchStudent(int id)
{
//confuses me
Student beingSearched = new Student();
beingSearched.setStudentId(id);
return this.searchStudent(beingSearched);
}
public boolean addStudent(Student student)
{
//confuses me
boolean added = false;
if (this.searchStudent(student) == null)
{
this.students.add(student);
added = true;
}
return added;
}
public boolean addStudent(int id, String name, double grade)
{
//this is fine as i know boolen and int, String and double//
Student student = new Student(id, name, grade);
return this.addStudent(student);
}