我如何实现 class 将文件加载到其他 classes 可以使用的我的程序中?

How do I implement a class that loads a file into my program that other classes can use?

好的,我正在构建一个程序来对学生的成绩和记录进行排序。它是一个命令行程序,当 运行 时,它将通过询问用户输入来启动。有exit(退出程序)、load [file name](加载一个文件名)、student [student name](加载学生记录)等几个命令,其他的不重要。好吧,基本上我想知道的和我坚持的是所有这些功能将在单独的 classes 中,并且会在用户输入特定命令时调用,但是如果我将 "load" 命令放入它自己的 class,那么我如何让它与其他 class 共享它的信息?我知道我必须使用 BufferReader 来读取文件,但我将如何实现我的负载 class,或者如果有更好的方法,请随意说。到目前为止,这是我的代码。我的其他 classes 没有太多内容,因为我觉得我需要先弄清楚如何读入文件并与其他 classes 共享文件。

import java.util.*;
import java.io.*;
public class program7
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Grade Stats by ");
        System.out.print(">");
        while(scan.hasNextLine())
        {

            String input = scan.nextLine();

            if(input.equals("exit"))
            {
                System.exit(0);
            }
            else if(input.equals("help"))
            {
                System.out.println("exit                   - program closes.");
                System.out.println("load [filename]        - loads a class database specified in [filename].");
                System.out.println("students               - prints out a list of students from the class, along ");
                System.out.println("                         with total points, and final grades for each student.");
                System.out.println("assignments            - prints out a list of assignments from the file, along with points possible");
                System.out.println("student [student name] - Prints report for the student");
                System.out.print(">");
            }
            else if(input.contains("load"))
            {
                String[] split = input.split(" ");
                Load load = new Load(split[1]);
                System.out.print(">");
            }
            else if(input.equals("students"))
            {

                System.out.print(">");
            }
            else if(input.equals("assignments"))
            {

                System.out.print(">");
            }
            else if(input.contains("student"))
            {
                String[] split = input.split(" ");
                Student student = new Student(split[1]);
                System.out.print(">");
            }
            else if(input.contains("assignment")
            {

            }
            else if(input.equals("grades")
            {

            }
            else
            {
                System.out.println("exit                   - program closes.");
                System.out.println("load [filename]        - loads a class database specified in [filename].");
                System.out.println("students               - prints out a list of students from the class, along ");
                System.out.println("                         with total points, and final grades for each student.");
                System.out.println("assignments            - prints out a list of assignments from the file, along with points possible");
                System.out.println("student [student name] - Prints report for the student");
                System.out.print(">");
            }
        }
    }

}

那是我的主要 class 这是目前为止 class 的功能。

public class Load
{
    public Load()
    {
        BufferReader in = new BufferReader
    }

}




public class Student
{
    public Student()
    {

    }

}


import java.util.*;
import java.io.*;
public class Student
{

    private String student;
    public Student(inputFile
    public Student(String student)
    {
        this.student = student;
    }

    public String Splitter()
    {

    }
    public String Printer()
    {

    }

}

Well basically what I am wondering and what I am stuck on is all those functions will be in separate classes and will be called when the user inputs a specific command,

but if I put the "load" command in its own class, then how do I get it to share its information with the other classes?

如果您的 Load class 有一个 public getter 方法,那么您可以通过这种方式检索它。

例如,在你的 Load class of

中使用一个方法
public String getFileContents() {
/* your BufferedFileReader and operations here */
    return s;
}

您可以通过在主 class 中使用

创建一个 Load 对象来访问它
Load load = new Load();
String s = load.getFileContents();

这样你就在你的主程序中创建了一个字符串class并从你的加载中提取数据class。

我希望这对您有所帮助并且已经回答了您的问题。

Can I access it in my other classes too, using the Load object? My other classes need some of the information because they process it differently. For example my students class, prints out the Students and their grades, while Student class will print out a certain student as specified by the user

所以你的 Student 中可能有一个方法 class 比如

public void printGrades(String fileContents) {
    /* do stuff */
    System.out.println("Grades");
}

然后通过

从您的主程序7 class调用它
Student student = new Student();
student.printGrades(s); // s is the String from load.getFileContents(); up above.

因此,您通过每个 class.

中的这些 public 方法将文件内容从加载 class 传递给学生 class