【问题标题】:How to use/override the readObject method,so that it is called inside the actionPerformed method? [closed]如何使用/覆盖 readObject 方法,以便在 actionPerformed 方法中调用它? [关闭]
【发布时间】:2014-08-28 06:52:21
【问题描述】:

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

【问题讨论】:

  • 你的问题不是很清楚。您能否向我们展示一些示例代码来说明您要做什么?
  • @andersschuller 已更新,请检查

标签: java serialization inputstream


【解决方案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();
}

输出:你好

您不能直接覆盖 readObject 它的最终方法。

【讨论】:

  • +1 final 签名很好
  • 但我已经看到了覆盖它的示例。
  • 你能发布你的覆盖示例的代码吗?
  • 我说的是 void readObject(ObjectInputStream)。
  • @Rouftantical 这是被反序列化的对象内部的私有 final 方法,而不是任何东西的覆盖。它会在你调用 ObjectInputStream.readObject() 时自动调用,你目前没有这样做。
猜你喜欢
  • 1970-01-01
  • 2011-11-03
  • 2022-01-23
  • 1970-01-01
  • 2021-04-22
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多