static 静态成员是不能进行序列化的,要自定义方法对其进行序列化,放入统一容器中!

<pre name="code" class="java">package Chapter18_IOSystem;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;

public class StoreCADStateS {
	public static void main(String[] args){
		ObjectInputStream ois=null;
		try {
			ois=new ObjectInputStream(new FileInputStream("src/Chapter18_IOSystem/CAD.txt"));
			List<Class<?extends Shape>>shapeTypesS=(List<Class<?extends Shape>>)ois.readObject();
			List<Shape>shapeS=(List<Shape>)ois.readObject();
			Line.deserializeStaticStatic(ois);
			System.out.println(shapeS);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(ois!=null)
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
}

abstract class Shape implements Serializable{
	public static int RED=1,YELLOW=2,BULE=3;
	private int xPos,yPos,dimension;
	private static Random random=new Random(47);
	private static int counter=0;
	public abstract void setColor(int newColor);
	public abstract int getColor();
	public Shape(int xPos,int yPos,int dimension){
		this.xPos=xPos;
		this.yPos=yPos;
		this.dimension=dimension;
	}
	public String toString(){
		return getClass()+" :color[ "+getColor()+" ] xPos[ "+xPos+" ] yPos ["+yPos+" ] dimension ["+dimension+" ];\n";
	}
	public static Shape shapeFactory(){
		int xPosS=random.nextInt(100);
		int yPosS=random.nextInt(100);
		int dimension=random.nextInt(100);
		switch(counter++%3){
		default:
		case 0:return new Circle(xPosS,yPosS,dimension);
		case 1:return new Square(xPosS,yPosS,dimension);
		case 2:return new Line(xPosS,yPosS,dimension);
		}
	}
}
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 color){
		this.color=color;
	}
	@Override
	public int getColor(){
		return color;
	}
}
class Square extends Shape{
	private int color;
	public Square(int xPos,int yPos,int dimension){
		super(xPos,yPos,dimension);
		color=RED;
	}
	@Override
	public void setColor(int color){
		this.color=color;
	}
	@Override
	public int getColor(){
		return color;
	}
}
class Line extends Shape{
	private static int color;
	public Line(int xPos,int yPos,int dimension){
		super(xPos,yPos,dimension);
	}
	/**
	 *为静态变量的序列化与反序列化准备的方法
	 * @param oos
	 * @throws IOException
	 */
	public static void serializeStaticState(ObjectOutputStream oos)throws IOException{
		//oos.writeInt(color);
	}
	public static void deserializeStaticStatic(ObjectInputStream ois)throws IOException{
		//color=ois.readInt();
	}
	@Override
	public void setColor(int color){
		this.color=color;
	}
	@Override
	public int getColor(){
		return color;
	}
}



 

输出:

Shapes: [class Chapter18_IOSystem.Circle :color[ 3 ] xPos[ 58 ] yPos [55 ] dimension [93 ];
, class Chapter18_IOSystem.Square :color[ 3 ] xPos[ 61 ] yPos [61 ] dimension [29 ];
, class Chapter18_IOSystem.Line :color[ 3 ] xPos[ 68 ] yPos [0 ] dimension [22 ];
, class Chapter18_IOSystem.Circle :color[ 3 ] xPos[ 7 ] yPos [88 ] dimension [28 ];
, class Chapter18_IOSystem.Square :color[ 3 ] xPos[ 51 ] yPos [89 ] dimension [9 ];
, class Chapter18_IOSystem.Line :color[ 3 ] xPos[ 78 ] yPos [98 ] dimension [61 ];
, class Chapter18_IOSystem.Circle :color[ 3 ] xPos[ 20 ] yPos [58 ] dimension [16 ];
, class Chapter18_IOSystem.Square :color[ 3 ] xPos[ 40 ] yPos [11 ] dimension [22 ];
, class Chapter18_IOSystem.Line :color[ 3 ] xPos[ 4 ] yPos [83 ] dimension [6 ];
, class Chapter18_IOSystem.Circle :color[ 3 ] xPos[ 75 ] yPos [10 ] dimension [42 ];
]
[class Chapter18_IOSystem.Circle :color[ 3 ] xPos[ 58 ] yPos [55 ] dimension [93 ];
, class Chapter18_IOSystem.Square :color[ 3 ] xPos[ 61 ] yPos [61 ] dimension [29 ];
, class Chapter18_IOSystem.Line :color[ 3 ] xPos[ 68 ] yPos [0 ] dimension [22 ];
, class Chapter18_IOSystem.Circle :color[ 3 ] xPos[ 7 ] yPos [88 ] dimension [28 ];
, class Chapter18_IOSystem.Square :color[ 3 ] xPos[ 51 ] yPos [89 ] dimension [9 ];
, class Chapter18_IOSystem.Line :color[ 3 ] xPos[ 78 ] yPos [98 ] dimension [61 ];
, class Chapter18_IOSystem.Circle :color[ 3 ] xPos[ 20 ] yPos [58 ] dimension [16 ];
, class Chapter18_IOSystem.Square :color[ 3 ] xPos[ 40 ] yPos [11 ] dimension [22 ];
, class Chapter18_IOSystem.Line :color[ 3 ] xPos[ 4 ] yPos [83 ] dimension [6 ];
, class Chapter18_IOSystem.Circle :color[ 3 ] xPos[ 75 ] yPos [10 ] dimension [42 ];
]


在另一个程序中恢复:

package Chapter18_IOSystem;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;

public class StoreCADStateS {
	public static void main(String[] args){
		ObjectInputStream ois=null;
		try {
			ois=new ObjectInputStream(new FileInputStream("src/Chapter18_IOSystem/CAD.txt"));
			List<Class<?extends Shape>>shapeTypesS=(List<Class<?extends Shape>>)ois.readObject();
			List<Shape>shapeS=(List<Shape>)ois.readObject();
			Line.deserializeStaticStatic(ois);
			System.out.println(shapeS);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(ois!=null)
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
}


输出:
[class Chapter18_IOSystem.Circle :color[ 1 ] xPos[ 58 ] yPos [55 ] dimension [93 ];
, class Chapter18_IOSystem.Square :color[ 3 ] xPos[ 61 ] yPos [61 ] dimension [29 ];
, class Chapter18_IOSystem.Line :color[ 0 ] xPos[ 68 ] yPos [0 ] dimension [22 ];
, class Chapter18_IOSystem.Circle :color[ 1 ] xPos[ 7 ] yPos [88 ] dimension [28 ];
, class Chapter18_IOSystem.Square :color[ 3 ] xPos[ 51 ] yPos [89 ] dimension [9 ];
, class Chapter18_IOSystem.Line :color[ 0 ] xPos[ 78 ] yPos [98 ] dimension [61 ];
, class Chapter18_IOSystem.Circle :color[ 1 ] xPos[ 20 ] yPos [58 ] dimension [16 ];
, class Chapter18_IOSystem.Square :color[ 3 ] xPos[ 40 ] yPos [11 ] dimension [22 ];
, class Chapter18_IOSystem.Line :color[ 0 ] xPos[ 4 ] yPos [83 ] dimension [6 ];
, class Chapter18_IOSystem.Circle :color[ 1 ] xPos[ 75 ] yPos [10 ] dimension [42 ];
]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值