正在反序列化ArrayList中的对象



我可以将arraylist序列化到文件,但在Java中从文件反序列化对象时遇到了问题。这是我的代码:

Shape.java

public interface Shape {
String toString();
}

矩形.java

import java.io.Serializable;
public class Rectangle implements Shape, Serializable {
private double length;
private double width;
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
@Override
public String toString() {
return "Rectangle{" +
"width=" + width +
", length=" + length +
'}';
}
}

Circle.java

import java.io.Serializable;
public class Circle implements Shape, Serializable {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public String toString() {
return "Circle{" +
"radius=" + radius +
'}';
}
}

Shapes.java

import java.io.Serializable;
import java.util.ArrayList;
public class Shapes implements Serializable {
private  ArrayList<Shape> shapeList = new ArrayList<>();
public Shapes() {
}
public Shapes(ArrayList<Shape> shapeList) {
this.shapeList = shapeList;
}
public ArrayList<Shape> getShapeList() {
return shapeList;
}
public void setShapeList(ArrayList<Shape> shapeList) {
this.shapeList = shapeList;
}
@Override
public String toString() {
return "Shapes{" +
"shapeList=" + shapeList +
'}';
}
public void add(Shape shape) {
shapeList.add(shape);
}
}

Main.java

import java.io.*;
import java.util.ArrayList;
public class Main extends Thread {
public static void main(String[] args) {
Shapes shapes = new Shapes();
Rectangle r1 = new Rectangle(5, 10);
Rectangle r2 = new Rectangle(12, 15);
Circle c1 = new Circle(5);
Circle c2 = new Circle(8);
shapes.add(r1);
shapes.add(r2);
shapes.add(c1);
shapes.add(c2);
// Serialization
try {
FileOutputStream streamOut = new FileOutputStream("./obj.ser");
ObjectOutputStream objectOutput = new ObjectOutputStream(streamOut);
objectOutput.writeObject(r1);
objectOutput.close();
streamOut.close();
} catch (IOException e) {
System.out.println(e);
}
System.out.println();
// DeSerialization
Shapes shapes2 = new Shapes();
try {
FileInputStream streamIn = new FileInputStream("./obj.ser");
ObjectInputStream objectInput = new ObjectInputStream(streamIn);
shapes2 = (Shapes) objectInput.readObject();
objectInput.close();
streamIn.close();
} catch (IOException | ClassNotFoundException e) {
System.out.println(e);
return;
}
System.out.println(shapes2.toString());
}
}

我不知道如何修复命令中显示的错误:

线程"main"java.lang.ClassCastException中的异常:class矩形不能强制转换为类Shapes(矩形和Shapes位于加载程序"app"的未命名模块(

序列化部分

objectOutput.writeObject(r1); // write a Rectangle

反序列化部分

shapes2 = (Shapes) objectInput.readObject(); // try to read Shapes instead of Shape or Rectangle

所以解决方案是:

Shape shape = (Shape) objectInput.readObject();

Rectangle r = (Rectangle) objectInput.readObject();

因为Rectangle不是Shapes而是Shape

最新更新