【发布时间】:2016-05-30 23:28:28
【问题描述】:
早上好/下午/晚上,
我是一名 Java 初学者,但我的任务是制作一个数据库应用程序。 在该应用程序中,我想创建一个在创建该类的实例时返回其他对象的类:
public LoadStudents(String query){
File studentsFolder = new File("Data/Students/");
try{
switch (query){
case "all": // additional cases might be added in the future
//- that's why I am using the class in the first place
for (final File entry : studentsFolder.listFiles()){
if(!entry.isDirectory()){
FileInputStream in = new FileInputStream(entry);
ObjectInputStream object = new ObjectInputStream(in);
Student[] student = (Student[]) object.readObject();
object.close();
}
}
break;
default:
final File entry = new File("Data/Students/"+query+".stud");
FileInputStream in = new FileInputStream(entry);
ObjectInputStream object = new ObjectInputStream(in);
Student searched = (Student) object.readObject();
}
}
catch(Exception load){
load.printStackTrace();
}
}
我的问题是:创建 LoadStudents 对象时可以访问学生对象吗?如果有,怎么做?
LoadStudents load = new LoadStudents("something");
load.searched.doSomething();
也许?
附:很抱歉,如果这是一个无聊的问题,但我还是想知道答案。
【问题讨论】:
-
尝试一下,看看它是否有效。你的想法似乎不错。另外,也许不要在构造函数中进行加载。这样你就可以有一个返回值。