java 反序列化时对象的static属性序列化问题。
2015-12-30 22:52
591 查看
代码仅供自己参考。
package com.zhi.learnj;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class StoreCadState {
public static void main(String[] args) throws Exception {
List<Class<? extends Shape>> shapeTypes = new ArrayList<>();
shapeTypes.add(Circle.class);
shapeTypes.add(Square.class);
shapeTypes.add(Line.class);
List<Shape> shapes = new ArrayList<>();
System.out.println("random generate ten shapes");
for(int i=0; i<10; i++){
shapes.add(Shape.randomFactory());
}
System.out.println("set color of all shapes to GREEN");
for(int i=0; i<10; i++){
shapes.get(i).setColor(Shape.GREEN);
}
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("cadstore.txt"));
oos.writeObject(shapeTypes);
Line.serializeStaticState(oos);
oos.writeObject(shapes);
oos.close();
System.out.println(shapes);
}
/**
random generate ten shapes
set color of all shapes to GREEN
[class com.zhi.learnj.Circle, color=3, xPos=58, yPos=55, dimension=93
, class com.zhi.learnj.Square, color=3, xPos=61, yPos=61, dimension=29
, class com.zhi.learnj.Line, color=3, xPos=68, yPos=0, dimension=22
, class com.zhi.learnj.Circle, color=3, xPos=7, yPos=88, dimension=28
, class com.zhi.learnj.Square, color=3, xPos=51, yPos=89, dimension=9
, class com.zhi.learnj.Line, color=3, xPos=78, yPos=98, dimension=61
, class com.zhi.learnj.Circle, color=3, xPos=20, yPos=58, dimension=16
, class com.zhi.learnj.Square, color=3, xPos=40, yPos=11, dimension=22
, class com.zhi.learnj.Line, color=3, xPos=4, yPos=83, dimension=6
, class com.zhi.learnj.Circle, color=3, xPos=75, yPos=10, dimension=42
]
*/
public static class RecoverCadState{
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
Square square = new Square(-1, -1, -1);
square.setColor(Shape.RED);
//如果不注释掉上面代码,Sqaure color = RED.
// 之前看书的时候没有注意到这部分。Class对象在一个jvm 中是唯一的,因此,在反序列化对象的时候,对象的static属性的值是当前jvm 中Class相关的static 值。
// 如Squre.color = RED; 因此在反序列化后,Squre 对象中所有的color 都是RED
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("cadstore.txt"));
List<Class<? extends Shape>> shapeTypes = (List<Class<? extends Shape>> 4000 ) List.class.cast(ois.readObject());
Line.deserializeStaticState(ois);
List<Shape> shapes = (List<Shape>) List.class.cast(ois.readObject());
System.out.println(shapes);
}
}
}
abstract class Shape implements Serializable{
public static final int RED = 1, BLUE = 2, GREEN = 3;
private static Random rand = new Random(47);
private static int counter = 0;
private int xPos, yPos, dimension;
public Shape(int xPos, int yPos, int dimension){
this.xPos = xPos;
this.yPos = yPos;
this.dimension = dimension;
}
public static Shape randomFactory(){
int xPos = rand.nextInt(100);
int yPos = rand.nextInt(100);
int dimension = rand.nextInt(100);
switch(counter++ % 3){
default:
case 0: return new Circle(xPos, yPos, dimension);
case 1: return new Square(xPos, yPos, dimension);
case 2: return new Line(xPos, yPos, dimension);
}
}
public abstract void setColor(int newColor);
public abstract int getColor();
@Override
public String toString() {
return getClass()+", color="+getColor()+", xPos="+xPos+", yPos="+yPos+", "
+ "dimension="+dimension+"\n";
}
}
class Line extends Shape{
private static int color = RED;
public Line(int xPos, int yPos, int dimension) {
super(xPos, yPos, dimension);
}
public static void serializeStaticState(ObjectOutputStream oos)
throws IOException{
oos.writeInt(color);
}
public static void deserializeStaticState(ObjectInputStream ois)
throws IOException{
color = ois.readInt();
}
@Override
public void setColor(int newColor) {
color = newColor;
}
@Override
public int getColor() {
return color;
}
}
class Square extends Shape{
private static int color;
public Square(int xPos, int yPos, int dimension) {
super(xPos, yPos, dimension);
color = RED;
}
@Override
public void setColor(int newColor) {
color = newColor;
}
@Override
public int getColor() {
return color;
}
}
class Circle extends Shape{
private static int color = RED;
public Circle(int xPos, int yPos, int dimension) {
super(xPos, yPos, dimension);
}
@Override
public void setColor(int newColor) {
color = newColor;
}
@Override
public int getColor() {
return color;
}
}
相关文章推荐
- java io (六) 对象序列化,反序列化 -- Serializable接口
- java对象的序列化和反序列化
- Java中对象的序列化和反序列化方法
- Java对象序列化和反序列化
- jackson中java对象中的属性名大小写问题
- 黑马程序员_Java对象的序列化和反序列化实践
- Java对象的序列化和反序列化实践
- Java对象的序列化和反序列化实践
- 转载 java控制对象的序列化和反序列化
- 关于将序列化的java对象保存到mysql数据库的一些问题
- Java对象的序列化和反序列化实践
- Java中对象的序列化和反序列化方法
- Java对象的序列化和反序列化实践
- java对象的序列化和反序列化
- Java对象的序列化和反序列化实践
- [Java] 对象的序列化和反序列化
- JAVA对象序列化,反序列化
- Java对象以Blob存储,及序列化反序列化 (1)
- 使用XStream对Java对象进行序列化和反序列化
- Java对象的XML序列化和反序列化 - (2)