有没有办法在actionPerformed中调用重写的readObject方法?由于无法显式调用,有没有办法做到这一点?这就是我想要做的: -
class ContinueActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
try {
FileInputStream input = new FileInputStream("Dpuzzle.sre");
ObjectInputStream fileInObject = new ObjectInputStream(input);
//GridGame game = (GridGame)(fileInObject.readObject()); i dont want to call readObject but instead i want to override it.
//fileInObject.close();
//StageSelect stages = new StageSelect();
//stages.setTheGame(game);
//stages.setVisible(true);
} catch (Exception exp) {
//no game to continue or file lost.
System.out.println("hell");
}
}
}
那么如何覆盖readObject
方法以便从actionPerformed
方法中调用它?
答案 0 :(得分:1)
您可以这样覆盖它:
public static void main(String[] args) throws IOException, ClassNotFoundException{
ObjectInputStream fileInObject = new ObjectInputStream(){
@Override
protected Object readObjectOverride() throws IOException,
ClassNotFoundException {
System.out.println("HELLO");
return null;
}
};
fileInObject.readObject();
}
输出:HELLO
您不能直接覆盖readObject,而是最终方法。