我开始与面向对象编程(OOP)和想知道:什么是序列化的意义在面向对象的说法?


当前回答

简单地说,序列化是一个将对象转换为字节流的过程,这样它就可以通过网络传输或存储在持久存储器中。

反序列化则完全相反——从网络或持久化存储中获取字节流,并将其转换回具有相同状态的Object。

需要理解的是如何解释或操作这些字节流,以便我们得到完全相同的对象/相同的状态。有很多方法可以实现这一点。其中一些是-

XML: Convert Object to XML, transfer it over a network or store it in a file/db. Retrieve it and convert it back to the object with same state. In Java we use JAXB(Java architecture for XML binding) library.(From java 6 it comes bundled with JDK). JSON: Same can be done by converting the Object to JSON (JavaScript Object notation). Again there is GSON library that can be used for this. Or we can use the Serialization that is provided by the OOP language itself. For example, in Java you can serialize an Object my making it implement Serializable interface and writing to Object Stream.

其他回答

序列化是将数据转换为由字节组成的线性“字符串”。

其他人或多或少也说过同样的事情,但我强调计算机模型要求数据适合一维寻址RAM或持久存储。

大多数“数据”本质上是可序列化的(即使你必须将抽象模型简化为线性模型);不可序列化的是网络连接或复杂的基于状态的机器,如解析器。

序列化是将无序数据(如对象)转换为一系列标记的过程,这些标记以后可用于重建原始数据。序列化形式通常是一个文本字符串,但不一定非得是这样。

When instantiating (constructing) the actual object(the thing) from a class (blueprint) there is a need to save the object (thing) by serializing it (breaking it down to its basic atomic structure) to a space in memory. (Kind of like Star Treks Transporter). You break the thing down into it stream of information that can be transported somewhere and stored. Then when you want to reconstruct the thing you just pull the atomically stored instance back into the object. Different from instaniation.

序列化是将内存中的对象转换为字节流的过程,这样您就可以将其存储在磁盘上或通过网络发送。

反序列化是相反的过程:将字节流转换为内存中的对象。

序列化必须将二进制对象转换为XML(或其他字符串)表示形式,以便将其存储在数据库/文件中或通过web服务调用通过网络发送。反序列化是相反的过程——将XML/字符串转换回对象。