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 ]; ]
相关文章推荐
- 为什么静态成员、静态方法(static)中不能用this和super关键字
- Fortran 从文件中读取数据放入数组中,然后对其进行bubble sort,使用了一些方法防止人为错误
- java中静态(static)的方法为什么不能重写(override)
- 为什么静态成员、静态方法中不能用this和super关键字
- spring注入时bean的set方法为什么不能是static类型的?(为什么要用单例模式而不是静态方法)
- java中static特殊性和final(static成员直接被访问,this不能用在static方法中,static不可访问非static)
- static--静态方法与静态成员
- 实例成员变量不能在静态方法中访问
- 为什么静态成员、静态方法中不能用this和super关键字
- C#中类的实例是不能 获取到类中的静态方法和静态变量(Static)的,及原因
- 为什么静态成员、静态方法中不能用this和super关键字
- C++里的静态(static)成员函数(不能用const的原因+static在c++中的第五种含义)
- PHP 类的静态成员属性与静态方法 static 关键字
- 成员内部类不能含有static的变量和方法
- C++里的静态成员函数(不能用const的原因+static在c++中的第五种含义)(转)
- 黑马程序员--03.Java面向对象--02.【static关键字】【main方法】【静态成员】
- 为什么静态成员、静态方法中不能用this和super关键字
- static(c中定义的关键字1、被其修饰者 外部无法使用(相反的都可以使用extern在外部使用) 2、函数局部变量使用成为静态的放入静态池-生存期改变。c++进行了扩充,使它在类中有了特殊作用)
- 为什么静态成员、静态方法中不能用this和super关键字
- java中static特殊性和final(static成员直接被访问,this不能用在static方法中,static不可访问非static)