下面是我困惑的4种方法,4种方法都在Teacher类中.我有一个学生班和老师班.在Teacher类中,声明的是ArrayList<Student> students实例变量.
如何解释我在下面给出的方法中看到的学生,它也被用作参数.我对Student 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;
} …Run Code Online (Sandbox Code Playgroud)