简体   繁体   English

在 java 文件 io 中序列化

[英]Serializing in java file io

Consider the following four hypothetical classes to be defined in a project: A, B, C, and D. A will be the parent class of B and C.考虑在项目中定义以下四个假设类:A、B、C 和 D。A 将是 B 和 C 的父 class。 C will have a data member variable of class D. In this project, we want to store C objects in a file using serialization approach, that is we want to create binary files that contain C objects. C will have a data member variable of class D. In this project, we want to store C objects in a file using serialization approach, that is we want to create binary files that contain C objects. Which classes of this project must implement the interface Serializable?这个项目的哪些类必须实现接口Serializable?

The C class should implement Serializable . C class 应该实现Serializable

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. class 的可串行化由实现 java.ZF98ED07A4D5F50F7DEFZ1410D0F50F7DEFZ1410D 接口的 class 启用。 Classes that do not implement this interface will not have any of their state serialized or deserialized.未实现此接口的类将不会对其 state 进行序列化或反序列化。

You cas read the official documentation of the interface:你cas阅读了接口的官方文档:

Interface Serializable 接口可序列化

On the other hand, for your D attribut you have to options:另一方面,对于您的 D 属性,您必须选择:

  • Implement Serializable, if you want to send it实现 Serializable,如果你想发送它
  • Make it transient , if you wan't to send it如果您不想发送它,请将其设为transient

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

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