[英]Need assistance explaining the purpose and operation of a specific method
考试前只看了一些编程问题,我对这个问题感到困惑...
关于附录A中详细介绍的Java
类,解释了matchProgrammeCode()
方法的目的和操作。 (10)
功能接口– 2个标记,本地等级– 3个标记,操作– 5个标记
附录A学生
public class Student {
int studentId;
String studentName;
String programmeCode;
Collection<ModuleRegistration> registeredModules;
static int noStudents = 0;
public Student() { … }
public Student(String studentName, String programmeCode) { … }
public Student(int studentId, String studentName,
String programmeCode) { … }
public int getStudentId(){ … }
public String getStudentName(){ … }
public void setStudentName(String studentName) { … }
public String getProgrammeCode(){ … }
public void setProgrammeCode(String programmeCode) { … }
public void addModule(ModuleRegistration moduleRegistration)
{ … }
@Override
public String toString() { … }
interface CheckStudent { boolean test(Student s); }
class CheckStudentProgrammeCode implements CheckStudent {
String programmeCode;
CheckStudentProgrammeCode(String programmeCode) {
this.programmeCode = programmeCode;
}
@Override
public boolean test(Employee s) {
return s.getProgrammeCode().equals(this.programmeCode);
}
}
public boolean matchProgrammeCode(String programmeCode) {
CheckStudentProgrammeCode tester =
new CheckStudentProgrammeCode(String programmeCode);
return tester.test(this);
}
}
任何帮助将不胜感激,谢谢。
这种方法实际上做无非是比较多programmeCode
您的实例的属性Student
你给它作为一个参数的字符串。 然后,您可以在主目录中这样使用它:
Student s = Student();
s.setProgrammeCode("Hello World");
if(s.matchProgrammeCode("Hello World")) {
/* Programmecode is equal :) */
}
很难解释您的代码中发生了什么,因为我不知道您在Java编程方面有多高级,但是我已经编辑了您的代码,以便仅保留问题的相关部分,并添加了一些注释,希望可以解释一下足够好:
public class Student {
String programmeCode;
public Student() { … }
// Everything that implements this interface will assure it has the method "test"
interface CheckStudent { boolean test(Student s); }
// Implements CheckStudent, therefore has to have the method "test"
// Also is an inner class of your Student-class
class CheckStudentProgrammeCode implements CheckStudent {
String programmeCode;
CheckStudentProgrammeCode(String programmeCode) {
// Constructor safes a string
this.programmeCode = programmeCode;
}
// Concrete implementation of the test-method which is required after implementing the interface
@Override
public boolean test(Employee s) {
// equals compares the programmeCode that was safed by the constructor with the s.getProgrammeCode() of the given object
// then returns the outcome of the equal method - true if they are equal, false if not
// It is equivalent to:
// if(s.getProgrammeCode().equals(this.programmeCode)) {return true;} else {return false;}
return s.getProgrammeCode().equals(this.programmeCode);
}
}
public boolean matchProgrammeCode(String programmeCode) {
// Creating a new, local instance of CheckStudentProgrammeCode and passing the
// String programmeCode directly from the parameter of the method into the instance's constructor
CheckStudentProgrammeCode tester =
new CheckStudentProgrammeCode(String programmeCode);
// The test-method takes the instance of Student itself (this) and returns a boolean, which will then be passed directly into the return of the method
return tester.test(this);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.