下面是我困惑的4种方法,4种方法都在Teacher类中.我有一个学生班和老师班.在Teacher类中,声明的是ArrayList< Student>学生作为实例变量.
如何解释我在下面给出的方法中看到的学生,它也被用作参数.我对学生searchStudent(在方法中)和学生(在论证中)非常困惑.这仅适用于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);
}
解决方法:
我建议你通过this link关于定义方法.
>公共学生搜索学生(学生)
它是一个返回Student类型对象的公共方法,它也接受Student类型的对象.
它需要接受student参数,因为它会搜索它.当您想要搜索记录中是否存在某个学生时(在学生ArrayList中),您将使用此方法.
> public Student searchStudent(int id)
同样,但它接受的参数是int.在这里,您将不是通过对象本身搜索学生,而是通过学生的ID搜索.
> public boolean addStudent(学生)
这是一种向学生ArrayList添加学生(学生类型)的方法.
提示:在调试模式下运行代码并按照您不理解的每种方法,您会惊讶于这将有助于您更好地理解程序的流程.
标签:java,class,methods
来源: https://codeday.me/bug/20190831/1776206.html
最后
以上就是如意睫毛膏最近收集整理的关于java形式参数是类名_java – 为什么方法使用类名作为修饰符(?)和参数?的全部内容,更多相关java形式参数是类名_java内容请搜索靠谱客的其他文章。
发表评论 取消回复