我检索到file
使用JFileChooser
我的Java程序的主要方法。我的问题是如何file
在程序的同一程序包中的不同类中访问它?
类可以以不同的方式进行交流,并且根据具体情况和体系结构选择正确的方式。我将把文件保存到您班级的一个字段中,并为该字段创建一个吸气剂。因此,您将能够访问其他类中的文件。
因此您的课程可能看起来像这样:
public class FileHolder {
private File file;
public File getFile() {
return this.file;
}
private void retrieveFile() {
// method which sets the file
}
// other methods and fields
}
和
public class FileUser {
private void doSomethingWithTheFile() {
FileHolder fileHolder = new FileHolder();
fileHolder.retrieveFile();
File file = fileHolder.getFile();
// use the file
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句