Java:Serializable无法保存对象';美国

Java:Serializable无法保存对象';美国,java,object,serialization,file-io,Java,Object,Serialization,File Io,我正在尝试用Java中的简单程序来保存对象状态,使用可序列化的,通过下面的代码,一旦我写下这些对象,然后将它们设为null,但是当我试图取回它们时,它会给我null值,它不应该给我存储的值吗当我运行此程序时,答案为空,请查看并指导我 Game.java public class Game implements Serializable{ public Game() { } public int getSize() { return size; }

我正在尝试用Java中的简单程序来保存对象状态,使用可序列化的,通过下面的代码,一旦我写下这些对象,然后将它们设为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();
        }

}

您的GameCharacter构造函数不调用游戏的构造函数,因此您正在传入被忽略的参数。尝试在原始对象上运行println()语句;您将获得相同的空值输出。修复此问题,序列化将正常工作。

您的GameCharacter构造函数不会为游戏调用构造函数,因此您正在传入被忽略的参数。尝试在原始对象上运行println()语句;您将获得相同的空值输出。修复此问题,序列化将正常工作。

正在序列化对象,但字段均为空。在游戏角色的构造函数中,您需要调用正确的超级构造函数,否则默认情况下,调用的构造函数是没有参数的默认构造函数

 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类,请不要将其用作变量名。

它正在序列化对象,但字段均为null。在游戏角色的构造函数中,您需要调用正确的超级构造函数,否则默认情况下,调用的构造函数是没有参数的默认构造函数

 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类,请不要将其用作变量名。

是的,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);
    }

是的,GameCharacter类扩展了游戏。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);
    }