java 反序列化时对象的static属性序列化问题。

代码仅供自己参考。


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>>) 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;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值