【发布时间】:2016-03-25 03:05:51
【问题描述】:
我想在我正在创建的名为“DocumentGenerator”的类中使用 java.nio.files.Path,但我不确定在使用不传入另一个 Path 对象的构造函数时如何实例化和初始化它.这是我的类变量和两个构造函数:
private ArrayList<String> totalOutput;
private ArrayList<String> programInput;
private Scanner in;
private String savePath, fileName;
private Path file;
public DocumentGenerator(Path file) {
this.programInput = new ArrayList<String>();
this.totalOutput = new ArrayList<String>();
this.in = new Scanner(System.in);
this.file = file;
this.savePath = "";
this.fileName = "";
}
public DocumentGenerator(String savePath, String fileName) {
this.programInput = new ArrayList<String>();
this.totalOutput = new ArrayList<String>();
this.in = new Scanner(System.in);
this.savePath = savePath;
this.fileName = fileName;
this.file =
}
在第二个构造函数中,savePath 和 fileName 在我将它们放入我的 Paths 对象之前需要进行一些操作,所以我还不想将它们传递给它。相反,我想尝试实例化和初始化“文件”以符合良好的编程习惯。我的问题是,根据This Question,Path 没有构造函数。在这种情况下,什么是好的编程实践? 可以在没有给定路径的情况下在我的构造函数中实例化和初始化它吗?
我的问题是不是“我该如何使用 java.nio.files.Path?”,我可以从Java API 找到。
【问题讨论】:
标签: java oop code-organization