【问题标题】:Java : Serializable not working to save Object's statesJava:可序列化无法保存对象的状态
【发布时间】:2013-07-23 07:32:33
【问题描述】:

我正在通过以下代码在Java to save objects state using Serializable 中尝试简单的程序,一旦我编写了这些对象然后将它们设置为 null 但when i try to get them back it gives me null values,它不应该给我存储的值吗? when i run this program answer is null null null,请查看并指导我,

Game.java

public class Game implements Serializable{

    public Game() {
    }
    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String getType() {
        return Type;
    }

    public void setType(String type) {
        Type = type;
    }

    public String[] getName() {
        return name;
    }

    public void setName(String[] name) {
        this.name = name;
    }

    int  size; 
    String Type ; 
    String [] name; 

    public   Game (int thesize , String theType, String[] names )
    {

        this.size = thesize; 
        this.Type= theType;
        this.name = names; 
    }

}

GameCharacter.java

public class GameCharacter extends Game {

    public GameCharacter(int i, String Type, String[] strings) {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Game g= new Game();

        GameCharacter GC1= new GameCharacter(1, "One " , new  String [] {"bow", "SWord", "Dust"});
        GameCharacter GC2 = new GameCharacter(2, "TWo", new String[] {"One ", "TWo ", "Three"});
        GameCharacter GC3= new GameCharacter(3, "Three", new String[] {"four ", "five ", "six"}); 

        ObjectOutputStream oos= new ObjectOutputStream (new FileOutputStream("Game.ser"));
        oos.writeObject(GC1);
        oos.writeObject(GC2);
        oos.writeObject(GC3);
        oos.close();          
        GC1= null;
        GC2= null; 
        GC3= null; 

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Game.ser"));

        try
        {
            GameCharacter onerestore= (GameCharacter) ois.readObject();
            GameCharacter tworestore  = (GameCharacter) ois.readObject();
            GameCharacter threerestore= (GameCharacter) ois.readObject();
            System.out.print(onerestore.getName());
            System.out.println(tworestore.getType());
            System.out.println(threerestore.getName());
            //System.out.println("HEllo");

        } 
        catch (ClassNotFoundException e) 
        { 
            e.printStackTrace();
        }

}

【问题讨论】:

    标签: java object serialization file-io


    【解决方案1】:

    是的,GameCharacter 类扩展了 Game。GameCharacter 中有一个构造函数。 所以 GameCharacter onerestore= (GameCharacter) ois.readObject() 会调用定义在子类 GameCharacter 中的构造函数。 但是看看这里

    public GameCharacter(int i, String Type, String[] strings) {
            // TODO Auto-generated constructor stub
        }
    

    是的,这个构造函数什么都不做。 所以这些字段仍然为空; 尝试如下改变构造函数,你可能会成功。

    public GameCharacter(int i, String Type, String[] strings) {
            // TODO Auto-generated constructor stub
            super( i ,Type,strings);
        }
    

    【讨论】:

      【解决方案2】:

      它正在序列化对象,但字段都是空的。在你的 GameCharacter 的构造函数中你需要调用正确的超级构造函数,否则默认调用的构造函数是没有参数的默认构造函数。

       public GameCharacter(int i, String Type, String[] strings) {
              // TODO Auto-generated constructor stub
             super(i, Type, strings); //you do not have this, hence your variables are null.
          }
      

      附带说明,Type 是一个 java 类,请不要将其用作变量名。

      【讨论】:

        【解决方案3】:

        您的 GameCharacter 构造函数不会调用 Game 的构造函数,因此您传入的参数只是被忽略。尝试在原始对象上运行 println() 语句;你会得到相同的空值输出。修复这个问题,序列化就可以正常工作了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-20
          • 2014-12-11
          • 2013-01-04
          • 1970-01-01
          相关资源
          最近更新 更多