简体   繁体   English

序列化具有不可序列化父 class 的 object

[英]Serializing an object which has a non-serializable parent class

How does the below code work?下面的代码是如何工作的?

     class A {
         int a = 10;
     }


     class B extends A implements Serializable{

      }



     public class Test {
       public static void main(String[] args){
        B obj = new B();
        obj.a = 25;


        //Code to serialize object B  (B b= new B()),
         // deserialize it and print the value of 'a'. 
      }
    }

The code prints 10 even though I have changed the value of 'a' in the code.即使我在代码中更改了“a”的值,代码也会打印 10。

Any explanation for this behaviour?对这种行为有什么解释吗?

The default value of a is 10 - it will be set to 10 when the object is created. a的默认值为 10 - 创建 object 时将设置为 10。 If you want to have a realistic test, set it to a different value after instantiation and then serialize it.如果要进行实际测试,请在实例化后将其设置为不同的值,然后将其序列化。

As for your update - if a class is not serializable, its fields are not serialized and deserialized.至于您的更新 - 如果 class 不可序列化,则其字段不会序列化和反序列化。 Only the fields of the serializable subclasses.只有可序列化子类的字段。

Since B extends A , it is an A .由于B扩展A ,因此它A This means that b instanceof Serializable returns true .这意味着b instanceof Serializable返回true

So, as long as the object you try to serialize returns true for the instanceof Serializable checks, you can serialize it.所以,只要你尝试序列化的object对于instanceof Serializable检查返回true,就可以序列化了。 This applies for any composite objects contained within this object itself.这适用于包含在此 object 本身中的任何复合对象。

But you can't do A a = new A();但是你不能做A a = new A(); and attempt to serialize a .并尝试序列化a .

Consider this:考虑一下:

java.lang.Object doesn't implement Serializable . java.lang.Object没有实现Serializable So, no one would've been able to serialize any objects in Java in that case, However.因此,在这种情况下,没有人能够序列化 Java 中的任何对象。 that's not the case at all, Also, in projects where there are multiple JavaBeans involved that extend a common super type, the general practice is to make this super type implement Serializable so that all the sub classes don't have to do that.根本不是这种情况,此外,在涉及多个扩展公共超类型的 JavaBeans 的项目中,一般做法是使此超类型实现Serializable以便所有子类都不必这样做。

If class B extends class A, and A is not serializable, then all the instance variables of class A are initialized with their default values (which is 10 in this case) after de-serialization of class B. If class B extends class A, and A is not serializable, then all the instance variables of class A are initialized with their default values (which is 10 in this case) after de-serialization of class B.

If you are a serializable class, but your superclass is NOT serializable, then any instance variables you INHERIT from that superclass will be reset to the values they were given during the original construction of the object.如果您是可序列化的 class,但您的超类不可序列化,那么您从该超类继承的任何实例变量都将重置为在 object 的原始构造期间给出的值。 This is because the non- serializable class constructor WILL run, In fact, every constructor ABOVE the first non-serializable class constructor will also run, no matter what, because once the first super constructor is invoked, (during deserialization).这是因为不可序列化的 class 构造函数将运行,事实上,第一个不可序列化 class 构造函数之上的每个构造函数也将运行,无论如何,因为一旦调用了第一个超级构造函数(在反序列化期间)。 it of course invokes its super constructor and so on up the inheritance tree.它当然会调用它的超级构造函数,等等 inheritance 树。

If a parent class is not serializable its fields are initialised each time the object is deserialized.如果父 class 不可序列化,则每次反序列化 object 时都会初始化其字段。 ie The object still needs to be constructed.即 object 仍然需要构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM