【问题标题】:How can I serialize an interface?如何序列化接口?
【发布时间】:2012-06-07 22:05:32
【问题描述】:

假设我有一个SerializableShapeHolder,它拥有一个实现Serializable Shape 接口的对象。我想确保保存正确的具体形状对象(并且稍后恢复正确的类型)。

我怎样才能做到这一点?

interface Shape extends Serializable {} 

class Circle implements Shape { 
   private static final long serialVersionUID = -1306760703066967345L;
}

class ShapeHolder implements Serializable {
   private static final long serialVersionUID = 1952358793540268673L;
   public Shape shape;
}

【问题讨论】:

    标签: java oop serialization types


    【解决方案1】:

    Java 的 Serializable 会自动为您执行此操作。

    public class SerializeInterfaceExample {
    
       interface Shape extends Serializable {} 
       static class Circle implements Shape { 
          private static final long serialVersionUID = -1306760703066967345L;
       }
    
       static class ShapeHolder implements Serializable {
          private static final long serialVersionUID = 1952358793540268673L;
          public Shape shape;
       }
    
       @Test public void canSerializeShape() 
             throws FileNotFoundException, IOException, ClassNotFoundException {
          ShapeHolder circleHolder = new ShapeHolder();
          circleHolder.shape = new Circle();
    
          ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test"));
          out.writeObject(circleHolder);
          out.close();
    
          ObjectInputStream in = new ObjectInputStream(new FileInputStream("test"));
          final ShapeHolder restoredCircleHolder = (ShapeHolder) in.readObject();
          assertThat(restoredCircleHolder.shape, instanceOf(Circle.class));
          in.close();
       }
    }
    

    【讨论】:

      【解决方案2】:
      import java.io.*;
      
      public class ExampleSerializableClass implements Serializable {
          private static final long serialVersionUID = 0L;
      
          transient private Shape shape;
          private String shapeClassName;
      
          private void writeObject(ObjectOutputStream out) throws IOException {
              shapeClassName = shape.getClass().getCanonicalName();
              out.defaultWriteObject();
          }
      
          private void readObject(ObjectInputStream in) 
                 throws IOException, ClassNotFoundException, 
                    InstantiationException, IllegalAccessException {
              in.defaultReadObject();
              Class<?> cls = Class.forName(shapeClassName);
              shape = (Shape) cls.newInstance();
          }
      }
      

      【讨论】:

      • Alexeyy,当您添加extends Serializale 时,所有这些工作都会自动完成。我在写这个问题时没有意识到这一点。我不确定您是否已经知道这一点并且对我的新手问题感到困惑。
      【解决方案3】:

      我想确保保存正确的具体形状对象(稍后恢复正确的类型)。

      Java 的默认序列化(ObjectInputStream 和 ObjectOutputStream)开箱即用。序列化时,Java 会在其中写入具体类的名称,然后在反序列化时使用。

      【讨论】:

      • 感谢 Esko,我做了figure that out 虽然花了几个小时试图重新发明轮子。 :)
      猜你喜欢
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多