Java打印();使用来自不同 class 的 getter 的方法

Java print(); method with getters from a different class

应学术机构要求删除 我无法提供示例

这是重要的错误信息。

Cannot make a static reference to the non-static method getStudentID(); from the type Student.

您需要在 class 的实例上调用 getStudentID(),而不是 class 本身。你可以尝试这样的事情。

public static void print_all() {

    System.out.println("Student ID\tRecent Grades\tName\t\tE-Mail\t\t\tAge");
    for (Student w : studentlist) {
        System.out.print(w.getStudentID() + "\t\t");
        System.out.print(w.getGrades() + "\t");
        System.out.print(w.getFirstname()+ " ");
        System.out.print(w.getLastname()+ "\t");
        System.out.print(w.getEmail()+ "\t");
        System.out.print(w.getAge()+ "\t");
        System.out.println(" ");
    }
}

调用 Student.getStudentID() 只有在所有学生都有一个静态(共享)ID 的情况下才有效。这里不是这种情况。您可以查看 this post 以获得对 java 中 static 关键字的更完整解释。

错误说明了一切。方法 getStudentID() 是 Student class 的非静态方法。调用 Student.getStudentID() 是静态调用,因此是错误。在 Student 的实例上调用方法 getStudentID()。