我正在尝试从我的超类创建一个子类的新实例。这是我的超级班
public abstract class Worker {
String world;
protected abstract void onLoad(Scanner read);
public static Worker load(Scanner read) {
// I want to create the instance of my sub class here and call it w
w.onLoad(read);
return w;
}
public void setWorld(String world) {
this.world = world;
}
}
这是我的子类
public class Factory extends Worker {
@Override
protected onLoad(Scanner read) {
setWorld(read.readline());
}
}
这就是我想要对这些课程做的事情。
public class MainClass{
public List<Factory> loadFactories() {
List<Factory> facts = new ArrayList<Factory>();
Scanner read = new Scanner(new FileInputStream("factory.txt"));
while(read.hasNextLine()) {
Factory f = (Factory)Factory.load(read);
facts.add(f);
}
read.close();
return facts;
}
}
有什么办法可以在不重新开始的情况下做到这一点?谢谢你的帮助。