Serializable

一、Shape类实现了Serializable,所有从Shape继承的类都会自动可序列化,

import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


abstract class Shape implements Serializable{
public static final int RED=1, BLUE=2, GREEN=3;
private int xPos, yPos, dimension;
private static Random r = new Random();
private static int counter = 0;
abstract public void setColor(int newColor);
abstract public int getColor();
public Shape(int xVal, int yVal, int dim) {
xPos = xVal;
yPos = yVal;
dimension = dim;
}
public String toSrting(){
return getClass().toString() + "color[" + getColor() + "] xPos["
+ xPos + "] yPos[" + yPos + "]dim[" + dimension + "]\n";
}
public static Shape randomFactory(){
int xVal = r.nextInt() % 100;
int yVal = r.nextInt() % 100;
int dim = r.nextInt() % 100;
switch (counter++ % 3) {
default:
case 0:return new Circle(xVal, yVal, dim);
case 1:return new Square(xVal, yVal, dim);
case 2:return new Line(xVal, yVal, dim);
}
}
}
class Circle extends Shape{
private static int color = RED;
public Circle(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
@Override
public void setColor(int newColor) {
color = newColor;
}
@Override
public int getColor() {
return color;
}

class Square extends Shape{
private static int color;
public Square(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
color = RED;
}
@Override
public void setColor(int newColor) {
color = newColor;
}
@Override
public int getColor() {
return color;
}
}
class Line extends Shape{
private static int color = RED;
public static void serilizeStaticState(ObjectOutputStream os) throws IOException{
os.writeInt(color);
}
public static void deserilizeStaticState(ObjectInputStream os) throws IOException{
color = os.readInt();
}
public Line(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
@Override
public void setColor(int newColor) {
color = newColor;
}
@Override
public int getColor() {
return color;
}
}
public class CADState {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
List shapeTypes, shapes;
if(args.length == 0){
shapeTypes = new ArrayList();
shapes =  new ArrayList();
shapeTypes.add(Circle.class);
shapeTypes.add(Shape.class);
shapeTypes.add(Line.class);
for(int i=0; i<10; i++){
shapes.add(Shape.randomFactory());
}
for(int i=0; i<10; i++){
((Shape)shapes.get(i)).setColor(Shape.GREEN);
}
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("CADState.out"));
out.writeObject(shapes);
}else{
ObjectInputStream is = new ObjectInputStream(new FileInputStream(args[0]));
shapeTypes = (List)is.readObject();
Line.deserilizeStaticState(is);
shapes = (List)is.readObject();
}
System.out.println(shapes);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值