开发者社区> 问答> 正文

java 序列化与解序列化

class DungeonTest{
public static void main(String[] args){
    DungeonGame d=new DungeonGame();

    System.out.println(d.getX()+d.getY()+d.getZ());  **//这个输出是12**

    try{
    FileOutputStream fos=new FileOutputStream("dg.ser");
    ObjectOutputStream oos=new ObjectOutputStream(fos);
    oos.writeObject(d);
    oos.close();

    FileInputStream fis=new FileInputStream("dg.ser");
    ObjectInputStream ois=new ObjectInputStream(fis);
    d=(DungeonGame)ois.readObject();
    ois.close();

    System.out.println(d.getX()+d.getY()+d.getZ()); **//这个输出是8**

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

    }

}
}
class DungeonGame implements Serializable{
public int x=3;
transient long y=4;
private short z=5;
int getX(){
    return x;
}
long getY(){
    return y;
}
short getZ(){
    return z;
}
}

2个输出分别为12,和8.
我搞不懂序列化的和解序列化的过程中,是哪里数据发生了变化。

展开
收起
蛮大人123 2016-03-19 14:16:18 1991 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    transient修饰的属性关闭了它的序列化,所以y=4没有被序列化,你取出来就像没有被初期化的样子,是0;所以反序列化后结果为8。

    2019-07-17 19:08:08
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载