使用getter一类我有读取一个文件转换成格式的类我想:当返回一个值或Java中
public class ProgramReader {
public List<String> readFromFile(String path){
List<String> commands = null;
try {
commands = Files.lines(Paths.get(getClass().getResource(path).toURI()))
.map(line -> line.split("\\s+")).flatMap(Arrays::stream)
.collect(Collectors.toList());
} catch (IOException e) {
System.err.println(e.getMessage());;
} catch (URISyntaxException e) {
e.printStackTrace();
}
return commands;
}
}
我传递的路径作为参数(而不是通过构造函数),因为,我不想实例化每次我改变路径。但是我对于返回结果感到困惑?我是否应该将其存储到类成员中,然后使用getter方法来访问结果?或者这个班级是否高效?
这要看情况。你试图达到什么目的? – Arlind
我没有看到任何问题,除了这个方法似乎并不涉及实例(这更多的是个人厌恶而不是实际的缺陷)。至于存储在一个字段var,这取决于你的应用程序需要什么 –
我想知道这实际上是否适用于面向对象的概念。这里我只是封装了一个函数。但是,那好吗? –