0

我没有编写此代码,所有功劳归于托管链接的人。所以这里是代码的链接,编写它的人也在 github 上列出...链接-Github SourceCode-

这是方法预览:(见下面的类)

//helper method
public static List readFile(String filePath) throws IOException {
    return new TextFileReader(filePath).readFile();
}

等一下,我想我现在可能明白了,只是有人可以在readFile()不依赖该对象的情况下调用该方法,还是我错了?

如果有人想要项目的较低级别视图,这里是 git 上项目的链接。我四处搜索,只看到辅助方法分解了更大的任务,这是我对它们的初步理解。但是,我在 github 上,发现了这个:(从页面底部提出的方法以便于查看,但也在类代码中。另外,如果有人想更好地查看,这里是 git 的链接......感谢您在页面底部的静态帮助方法之前看起来正常的任何回复或编辑

public class TextFileReader implements FileReaderStrategy<String> {

private static final FileType FILE_TYPE = FileType.TEXT_FILE;
private String filePath;

public TextFileReader(String filePath) {
    this.filePath = filePath;
}

@Override
public List<String> readFile() throws IOException {
    List lines = new ArrayList();
    BufferedReader in = null;
    try {
        in = new BufferedReader(
                new FileReader(filePath));
        String line = in.readLine();
        while (line != null) {
            lines.add(line);
            line = in.readLine();
        }
    } catch(IOException ioe){
        throw ioe;
    }finally{
        if(in != null){
            in.close();
        }
    }
    
    return lines;
}

//helper method
public static List readFile(String filePath) throws IOException {
    return new TextFileReader(filePath).readFile();
}

}

4

1 回答 1

1

你是对的。尽管我会说这样可以在类的对象或实例上Class而不是在类的对象或实例上调用该方法。

例如,静态方法可以这样调用:

TextFileReader.readFile(<filepath>);

无需先创建类的实例。

于 2014-05-08T02:37:14.887 回答