java 如何序列化接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10871044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:54:58  来源:igfitidea点击:

How can I serialize an interface?

javaoopserializationtypes

提问by Jeff Axelrod

Suppose I have a Serializableclass ShapeHolderthat owns an object that implements a SerializableShapeinterface. I want to make sure the correct concrete shape object is saved (and the correct type is later restored).

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

How can I accomplish this?

我怎样才能做到这一点?

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;
}

采纳答案by Jeff Axelrod

Java's Serializabledoes this for you automatically.

JavaSerializable会自动为您执行此操作。

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();
   }
}

回答by Esko Luontola

I want to make sure the correct concrete shape object is saved (and the correct type is later restored).

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

Java's default serialization (ObjectInputStream & ObjectOutputStream) does that out-of-the-box. When serializing, Java writes there the name of the concrete class and then uses in when deserializing.

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

回答by Alexeyy Alexeyy

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();
    }
}