谁实际实现了可序列化方法? [英] Who actually implements serializable methods?

查看:197
本文介绍了谁实际实现了可序列化方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习如何使用 Serializable

I've been learning how to use Serializable.

我知道如果我创建了一个类'A '使用不同的变量来实现 Serializable 并且我将 Serializable 添加到我的班级,它也是 Serializable

I know if I create a class 'A' with different variables who implements Serializable and I add Serializable to my class, it's also Serializable.

但是,谁实际实现了这两种方法来序列化?
是否对象处理所有事情或不同的类在必要时重载它们?

But, who is actually implementing those two methods to serialize? Does Object take care of everything or different classes overloads them when necessary?

推荐答案

序列化实际上是在 java.io.ObjectOutputStream (和java.io.ObjectInputStream)及其一些辅助类中实现的。在许多情况下,这种内置支持就足够了,开发人员只需要实现标记接口 Serializable 。这个接口被称为标记,因为它没有声明任何方法,因此在实现类上不需要任何特殊的API。

The serialization is actually implemented in java.io.ObjectOutputStream (and java.io.ObjectInputStream) and some of its helper classes. In many cases, this built-in support is sufficient, and the developer simply needs to implement the marker interface Serializable. This interface is called a "marker" because it doesn't declare any methods, and thus doesn't require any special API on implementation classes.

程序员可以添加或如果需要,使用自己的方法替换默认序列化机制。例如,如果在反序列化对象后需要一些额外的初始化,则可以使用以下签名添加方法:

A programmer can add or replace default serialization mechanism with their own methods if needed. For example, if some additional initialization is required after deserializing an object, a method can be added with the following signature:

private void readObject(java.io.ObjectInputStream s)
               throws java.io.IOException, java.lang.ClassNotFoundException

要完全控制序列化和反序列化,请实现 java.io.Externalizable 而不是 Serializable

For total control over serialization and deserialization, implement java.io.Externalizable instead of Serializable.

如果需要,Java序列化还有许多其他扩展点。 序列化规范是权威且完整的来源了解所有这些。

There are many other extension points in Java serialization, if needed. The serialization specification is an authoritative and complete source for learning about all of them.

这篇关于谁实际实现了可序列化方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆