简体   繁体   English

将变量从一个类传递到另一个类,而无需更改方法名称

[英]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.

相关问题 将变量从一种方法传递到另一种方法? - Passing a variable from one method to another method? 将变量的值从一种方法传递到另一种方法 - Passing value of variable from one method to another 在java中将变量从一个类传递到另一个类 - Passing a variable from one class to another in java 将变量从一个类传递到另一个类 - Passing the variable from one class to another 我的方法(正在被另一个类调用)如何从该类获取变量而不将其作为参数传递? - How can my method, which is being called by another class, obtain a variable from that class without passing it as a parameter? 将变量从 JFrame Class 的私有方法传递给另一个 Jframe ZA2F2ED4F8EBC2CBB4C21A29DC40AB6D - Passing a variable from private method of JFrame Class to another Jframe class 如何避免将一个变量从一种方法传递给另一种方法 - How to avoid passing one variable from one method to another 在Java中将变量值从一个类传递到另一个类 - Passing a variable value from one class to another class in java 将方法作为参数从一个类传递给Java中另一个类的构造函数 - passing a method as an argument from one class to the constructor of another class in java 从一种方法传递给另一种方法时变量不一致 - inconsistent variable when passing it from one method to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM