什么时候应该实现Serializable接口? [英] When should we implement Serializable interface?

查看:520
本文介绍了什么时候应该实现Serializable接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Contact implements Serializable {
    private String name;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}




  1. 我应该何时实施 Serializable 界面?

  2. 为什么我们这样做?

  3. 它是否有任何优势或安全性?

  1. When should I implement Serializable interface?
  2. Why do we do that?
  3. Does it give any advantages or security?


推荐答案


  1. 来自这个序列化的内容是什么?


它允许你获取一个对象或一组
对象,将它们放在磁盘上或通过有线或无线
传输机制发送
,然后再用另一个
计算机,反向
过程:恢复原来的
对象。基本机制是将
将对象展平为
一维比特流,并将
转换回
原始对象的比特流。

It lets you take an object or group of objects, put them on a disk or send them through a wire or wireless transport mechanism, then later, perhaps on another computer, reverse the process: resurrect the original object(s). The basic mechanisms are to flatten object(s) into a one-dimensional stream of bits, and to turn that stream of bits back into the original object(s).

就像星际迷航中的转运者一样,
所有这些都是关于将
复杂化并将其变为平坦的
序列的1s和0,然后取
序列1和0(可能在另一个地方
,可能在另一个
时间)并重建原始的
复杂的东西。

Like the Transporter on Star Trek, it's all about taking something complicated and turning it into a flat sequence of 1s and 0s, then taking that sequence of 1s and 0s (possibly at another place, possibly at another time) and reconstructing the original complicated "something."

因此,当您需要存储对象的副本时,实现 Serializable 界面,将它们发送到另一个在同一系统或网络上运行的进程。

So, implement the Serializable interface when you need to store a copy of the object, send them to another process which runs on the same system or over the network.

因为你想存储或发送一个对象。

Because you want to store or send an object.

它使存储和发送对象变得容易。它与安全性无关。

It makes storing and sending objects easy. It has nothing to do with security.

这篇关于什么时候应该实现Serializable接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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