为什么readObject和writeObject是私有的,为什么我会明确地写出瞬态变量? [英] Why are readObject and writeObject private, and why would I write transient variables explicitly?

查看:825
本文介绍了为什么readObject和writeObject是私有的,为什么我会明确地写出瞬态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Effective Java 中有关序列化的章节。

I am reading the chapter on Serialization in Effective Java.


  1. 谁调用了readObject ()和writeObject()?为什么这些方法被宣告为私有?

  1. Who calls the readObject() and writeObject()? Why are these methods declared private ?

以下是本书中的一段代码

The below is a piece of code from the book

// StringList with a reasonable custom serialized form
public final class StringList implements Serializable {
    private transient int size = 0;
    private transient Entry head = null;

    //Other code

    private void writeObject(ObjectOutputStream s)
        throws IOException {
        s.defaultWriteObject();
        s.writeInt(size);
        // Write out all elements in the proper order.
        for (Entry e = head; e != null; e = e.next)
           s.writeObject(e.data);
        }
    }
}

是否有任何具体原因变量 size 被声明为transient,然后在writeObject方法中显式写入?如果它没有被声明为瞬态,那么无论如何都会被写入,对吧?

Is there any specific reason the variable size is declared as transient and then in the writeObject method it is explicitly written? If it were not declared as transient, it would have been written anyway, right?


推荐答案

(1)方法未在任何类或接口中声明。一个实现 Serializable 接口的类,需要 在序列化和反序列化过程中的特殊处理必须实现 这些方法,并且序列化器/反序列化器将尝试反映那些方法。

(1) The methods are not declared in any class or interface. A class, that implements the Serializable interface and requires special special handling during the serialization and deserialization process must implement those methods and the serializer/deserializer will try to reflect those methods.

这是Java中相当奇怪的角落之一,其中API实际上是在javaDoc中定义的......但 if 方法已在接口中定义,然后他们 public (我们无法通过添加<来实现接口方法锁定它code>私人修饰符)。

This is one of the rather strange corners in Java where the API is actually defined in the javaDoc... But if the methods had been defined in an interface, then they had to be public (we can't implement an interface method an lock it by adding a private modifier).

为什么私有 - javaDoc没有给出提示。也许它们被指定为为私有,因为除了实现者之外没有其他类可以使用它们。根据定义,它们私有

Why private - the javaDoc does not give a hint. Maybe they are specified as private because no other class but the implementor is intended to use them. They are private by definition.

(2)该示例仅显示特殊处理的如何。在此示例中, size 是暂时的,不会被序列化。但是现在我们引入了特殊处理程序,这个处理程序将 size 的值添加到流中。与非瞬态字段的常规方法的不同之处可能是结果流中元素的顺序(如果它很重要......)。

(2) The example simply shows how the special handling works. In this example, size is transient and will not be serialized. But now we introduce the special handler and this handler adds the value of size to the stream. The difference to the normal approach with non-transient fields could be the order of elements in the resulting stream (if it matters...).

这个例子可能有意义,如果在超类中定义了瞬态字段,并且子类想要序列化该值。

The example could make sense, if the transient field was defined in a super class and a subclass wanted to serialize the value.

这篇关于为什么readObject和writeObject是私有的,为什么我会明确地写出瞬态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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