gpt4 book ai didi

java - 可串行化和可外部化。反序列化时构造函数调用的区别

转载 作者:行者123 更新时间:2023-11-30 03:37:03 25 4
gpt4 key购买 nike

我已阅读以下文章:

http://javapapers.com/core-java/externalizable-vs-serializable/

In object de-serialization (reconsturction) the public no-argument constructor is used to reconstruct the object. In case of Serializable, instead of using constructor, the object is re-consturcted using data read from ObjectInputStream.
The above point subsequently mandates that the Externalizable object must have a public no-argument constructor. In the case of Seriablizable it is not mandatory.

构造函数调用是否属实

可序列化:
而反序列化仅调用最近的非 Serialized 祖先的构造函数

可外部化:
而反序列化则调用仅实现 Externalized 接口(interface)的类的构造函数。

最佳答案

是的,在字节代码中,您可以创建对象的实例并调用层次结构中的任何构造函数。事实上,构造函数是一种特殊的方法,甚至会多次调用它。

许多反序列化器只使用 Unsafe.allocateInstance() 并且不调用任何构造函数。这样做是为了最大限度地减少反序列化时的副作用。

关于java - 可串行化和可外部化。反序列化时构造函数调用的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27616585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com