static 静态成员是不能进行序列化的,要自定义方法对其进行序列化,放入统一容器中!
来源:互联网 发布:黑马餐饮软件 编辑:程序博客网 时间:2024/05/01 23:32
<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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(ois!=null)try {ois.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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);}@Overridepublic void setColor(int color){this.color=color;}@Overridepublic 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;}@Overridepublic void setColor(int color){this.color=color;}@Overridepublic 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();}@Overridepublic void setColor(int color){this.color=color;}@Overridepublic 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(ois!=null)try {ois.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 0
- static 静态成员是不能进行序列化的,要自定义方法对其进行序列化,放入统一容器中!
- DataRow放入ViewState中不能序列化的问题
- java-Java可以对方法进行序列化?
- PHP对变量进行序列化的方法大比拼 四种序列化变量的方法
- Unity 中使用 ScriptableObject 对数据进行序列化
- JAVA中对类进行序列化Serializable
- Android开发之使用Parcelable对自定义的类进行序列化
- 当很频繁地对序列中部进行插入和删除操作时,应该选择使用的容器是()
- ScriptableObject进行序列化
- 使用序列化方法进行sql行转列
- 怎样对带有不可序列化属性的Java对象进行序列化
- python中使用pickle进行序列化
- python中使用pickle进行序列化
- java中是不能对数组进行强制转换的
- 通用序列化基类 只须实现该类抽象成员即可进行序列化与反序列化
- 不能对非静态字段/方法进行静态引用
- 使用BinaryFormatter进行序列化
- 使用SoapFormatter进行序列化
- 黑马程序员_交通灯管理系统(2)
- OpenCV学习笔记1 OpenCV软件开发环境的搭建
- 运动会分数统计
- centos6.0 配置SVN
- overflow属性 -- css属性详解系列(2)
- static 静态成员是不能进行序列化的,要自定义方法对其进行序列化,放入统一容器中!
- NYOJ-791 Color the fence
- Ubuntu12.04密码正确 进不去桌面系统(已测试恢复正常)
- 求两个有序数组的中值
- java 冒泡排序
- 用BAT批量修改文件名方法
- WIN7安装最新版VirtualBox报错“创建 COM 对象失败”解决方法
- 黑马程序员 C语言一些常见的注意6(尾)
- android devlop:adb如何使用 &&& 消除工程导入时的“Invalid project description”错误