您的位置:首页 > 其它

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

2014-06-29 18:44 363 查看
<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 ];
]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐