Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hazelcast VersionedPortable如何处理类继承_Java_Hazelcast - Fatal编程技术网

Java Hazelcast VersionedPortable如何处理类继承

Java Hazelcast VersionedPortable如何处理类继承,java,hazelcast,Java,Hazelcast,所以我们使用VersionedPortable作为Hazelcast的协议,我们知道在序列化/反序列化时字段的顺序很重要。我们不了解的是如何处理序列化/反序列化类中的继承 Class A { int one; int two; void writePortable( PortableWriter writer ) { writer.writeInt( "one", one ); writer.writeInt( "two", three ); } } 现在

所以我们使用VersionedPortable作为Hazelcast的协议,我们知道在序列化/反序列化时字段的顺序很重要。我们不了解的是如何处理序列化/反序列化类中的继承

Class A {
  int one;
  int two;

  void writePortable( PortableWriter writer ) {
     writer.writeInt( "one", one );
     writer.writeInt( "two", three );
  }
}

现在让我们假设我们在类a中添加一个名为“四”的字段-然后顺序被打破。4将在2和3之间序列化,这是不正确的。我能想到的唯一选择是只在叶类中实现写/读方法,但随着时间的推移,维护起来会非常困难。此外,如果您有许多叶类,则很可能是错误的。有没有更好的方法解决这个问题?

如果您计划更改层次结构,那么您可能应该计划更长期的实施。例如,使writePortable在类中成为final方法,这样子类就不会实现它。而是定义一个抽象(或空impl)writePortableSubcl()方法。这样,A类的writePortable看起来像:

公共最终无效可写端口(PortableWriter,…){ //1)写下我的已知属性

// 2) call ...
writePortableSubclass(writer);

// 3) write my remaining added attributes

// as you add more attributes to subclasses, you'll have to
// follow this pattern to keep attributes in order.  There is no magic api to do this for you.
}

// 2) call ...
writePortableSubclass(writer);

// 3) write my remaining added attributes

// as you add more attributes to subclasses, you'll have to
// follow this pattern to keep attributes in order.  There is no magic api to do this for you.