java 序列化具有不可序列化父类的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6649348/
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-30 16:47:42  来源:igfitidea点击:

Serializing an object which has a non-serializable parent class

javaserialization

提问by Vinoth Kumar C M

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 ?

这种行为有什么解释吗?

回答by Bozho

The default value of ais 10 - it will be set to 10 when the object is created. If you want to have a realistic test, set it to a different value after instantiation and then serialize it.

的默认值为a10 - 创建对象时将设置为 10。如果要进行实际测试,请在实例化后将其设置为不同的值,然后对其进行序列化。

As for your update - if a class is not serializable, its fields are not serialized and deserialized. Only the fields of the serializable subclasses.

至于您的更新 - 如果一个类不可序列化,则其字段不会被序列化和反序列化。只有可序列化子类的字段。

回答by adarshr

Since Bextends A, it is anA. This means that b instanceof Serializablereturns true.

由于Bextends A,它是一个A. 这意味着b instanceof Serializable返回true.

So, as long as the object you try to serialize returns true for the instanceof Serializablechecks, you can serialize it. This applies for any composite objects contained within this object itself.

因此,只要您尝试序列化的对象在instanceof Serializable检查中返回 true ,您就可以对其进行序列化。这适用于该对象本身中包含的任何复合对象。

But you can't do A a = new A();and attempt to serialize a.

但是你不能做A a = new A();并尝试序列化a.

Consider this:

考虑一下:

java.lang.Objectdoesn't implement Serializable. So, no one would've been able to serialize any objects in Java in that case! However, 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 Serializableso that all the sub classes don't have to do that.

java.lang.Object不执行Serializable。因此,在这种情况下,没有人能够序列化 Java 中的任何对象!然而,事实并非如此。此外,在涉及多个 JavaBean 扩展公共超类型的项目中,一般的做法是实现这个超类型,Serializable以便所有子类都不必这样做。

回答by Sandy

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.

如果类 B 扩展了类 A,并且 A 不可序列化,则在类 B 反序列化后,类 A 的所有实例变量都将使用它们的默认值(在本例中为 10)进行初始化。

回答by Vasu

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. 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), it of course invokes its super constructor and so on up the inheritance tree.

如果您是一个可序列化的类,但您的超类不可序列化,那么您从该超类继承的任何实例变量都将重置为它们在对象的原始构造期间给定的值。这是因为不可序列化的类构造函数将运行!事实上,第一个不可序列化的类构造函数上面的每个构造函数也将运行,无论如何,因为一旦调用第一个超级构造函数(在反序列化期间),它当然会调用它的超级构造函数,依此类推继承树。

回答by Peter Lawrey

If a parent class is not serializable its fields are initialised each time the object is deserialized. i.e. The object still needs to be constructed.

如果父类不可序列化,则每次反序列化对象时都会初始化其字段。即对象仍然需要构造。