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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 字花36种动物详细资料 三十六字花谜语破解软件 三十六动物字花手册 字花题目及答案 100万存银行一年利息多少 10万存一年利息多少 2019存30万存三年利息多少 50万存银行一年多少利息 1000万存银行一年利息多少 200万存银行一年利息多少 10万存银行好还是余额宝好 十万存银行一年多少利息 一万存一年利息多少 五十万存银行一年有多少利息 40万存银行一年利息多少 20万存定期三年利息 二十万存定期一年利息多少 20万存银行一年利息是多少 10万存3年定期利息是多少 100万存银行一个月利息多少 20万存余额宝一天收益多少 十万存一年定期利息是多少 十万存定期一年利息多少 10万存银行一年利息多少 一万存三个月利息多少 15万存银行一年利息多少 十万存余额宝一年收益多少 5万存银行一年利息多少 2018存款20万存3年利息会有多少 大额存单20万存一年有多少钱 10万存定期还是买理财 3万存银行一年利息多少 800万存银行一年利息多少 2万存银行一年利息多少 1000万存银行一年多少利息 一万存余额宝一天利息 三十万存银行一年利息多少 2018年20万存3年定期利息多少钱 5万存余额宝一天多少钱 20万存支付宝一天有多少利息 十万存三年定期利息是多少