如何使用/覆盖readObject方法,以便在actionPerformed方法中调用它?

时间:2014-07-07 17:39:26

标签: java serialization inputstream

有没有办法在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方法中调用它?

1 个答案:

答案 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,而是最终方法。