[英]passing a variable from one class to another, without altering method names
I have a class Course that needs to pass a JUnit case in a test class, therefore the method name getNumberStudents needs to remain the same. 我有一个课程Course,需要在测试类中传递JUnit案例,因此方法名称getNumberStudents需要保持不变。
public class Course {
private Students students = null;
public Course(Students students, Grades grades) {
this.students = students;
}
public int getNumberStudents() {
int numberStudents = 0;
numberstudents = students.getNumberStudents();
return numberStudents;
}
However, I need to access the data using another class, 'Students'. 但是,我需要使用另一个类“学生”来访问数据。 Due to how the test class is setup, I can't modify the method signature in this class either. 由于测试类的设置方式,我也无法在此类中修改方法签名。 Can someone please tell me how I can pass the result I am printing out in the Students class to the Course class? 有人可以告诉我如何将我在“学生”课程中打印出的结果传递给“课程”课程吗?
public class Students {
public Students(String studentsDb) {
try {
FileInputStream file = new FileInputStream(new File(studentsDb));
XSSFWorkbook workbook = new XSSFWorkbook(file);
int numberStudents = 0;
// Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row.getRowNum() == 0) {
continue;
}
numberStudents = numberStudents + 1;
}
System.out.println(numberStudents);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Or, is there a simpler way to do this? 或者,是否有更简单的方法来做到这一点? Thanks. 谢谢。
Can you do something like: 你能做类似的事情吗?
public Class Students {
private int numStudents;
public Students(String studentsDb) {
//Keep everything the same
//add this line under the print statement:
this.numStudents = numberStudents;
}
public int getNumberStudents() {
return numStudents;
}
}
Then in your Course
class you can now do students.getNumberStudents();
然后,在Course
您现在可以做students.getNumberStudents();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.