结合源码进行分析
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.io;
/**
* Serializability of a class is enabled by the class implementing the
* java.io.Serializable interface. Classes that do not implement this
* interface will not have any of their state serialized or
* deserialized. All subtypes of a serializable class are themselves
* serializable. The serialization interface has no methods or fields
* and serves only to identify the semantics of being serializable.
* 类的可序列化性由实现java.io.serializable接口的类启用。不实现此接口的类将不具有序列化或反序列化的任何状态。可序列化 * 类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。
*
* To allow subtypes of non-serializable classes to be serialized, the
* subtype may assume responsibility for saving and restoring the
* state of the supertype's public, protected, and (if accessible)
* package fields. The subtype may assume this responsibility only if
* the class it extends has an accessible no-arg constructor to
* initialize the class's state. It is an error to declare a class
* Serializable if this is not the case. The error will be detected at
* runtime.
* 要允许序列化不可序列化类的子类型,子类型可能会负责保存和还原父类型的公共、受保护和(如果可访问)包字段的状态。
* 只有它所扩展的类具有可访问的无参数构造函数来初始化类的状态时,子类型才可以承担此责任。如果不是这样,则声明类
* 可序列化是错误的。运行时将检测到错误。
* 比如:
* 1.在序列化一个子类对象的时候,如果它的父类没有实现Serializable接口,那么该子类将只会保存子类型本身定义的属性
* 当时所持有的状态,父类中的属性所持有的状态不会进行保存。
* 2.在进行反序列化的时候,为了构造子类对象,就必须先构造父类对象,而如果父类没有实现Serializable接口,那么将会调用
* 父类的无参构造函数作为默认的父类对象,如果是这种情况,就需要考虑在父类的无参构造函数中对变量值进行初始化。
*
* During deserialization, the fields of non-serializable classes will
* be initialized using the public or protected no-arg constructor of
* the class. A no-arg constructor must be accessible to the subclass
* that is serializable. The fields of serializable subclasses will
* be restored from the stream.
* 在反序列化过程中,将使用类的public或protected 无参构造函数初始化不可序列化类的字段。可以序列化的子类必须可以访
* 问无参构造函数。可序列化子类的字段将从流中还原。
*
* When traversing a graph, an object may be encountered that does not
* support the Serializable interface. In this case the
* NotSerializableException will be thrown and will identify the class
* of the non-serializable object.
* 遍历图形时,可能会遇到不支持可序列化接口的对象。在这种情况下,将引发NotSerializableException,并标识不可序列化
* 对象的类。
*
* Classes that require special handling during the serialization and
* deserialization process must implement special methods with these exact
* signatures:
* 在序列化和反序列化过程中需要特殊处理的类必须实现具有这些确切签名的特殊方法:
*
* private void writeObject(java.io.ObjectOutputStream out)
* throws IOException
* private void readObject(java.io.ObjectInputStream in)
* throws IOException, ClassNotFoundException;
* private void readObjectNoData()
* throws ObjectStreamException;
*
The writeObject method is responsible for writing the state of the
* object for its particular class so that the corresponding
* readObject method can restore it. The default mechanism for saving
* the Object's fields can be invoked by calling
* out.defaultWriteObject. The method does not need to concern
* itself with the state belonging to its superclasses or subclasses.
* State is saved by writing the individual fields to the
* ObjectOutputStream using the writeObject method or by using the
* methods for primitive data types supported by DataOutput.
* WriteObject方法负责为其特定类写入对象的状态,以便相应的ReadObject方法可以还原它。通过调用可以调用保存对象字段的
* 默认机制。该方法不需要关注属于它的超类或子类的状态。通过使用WriteObject方法将单个字段写入
* out.defaultWriteObjectObjectOutputStream或使用DataOutput支持的基元数据类型的方法来保存状态。
*
*
The readObject method is responsible for reading from the stream and
* restoring the classes fields. It may call in.defaultReadObject to invoke
* the default mechanism for restoring the object's non-static and
* non-transient fields. The defaultReadObject method uses information in
* the stream to assign the fields of the object saved in the stream with the
* correspondingly named fields in the current object. This handles the case
* when the class has evolved to add new fields. The method does not need to
* concern itself with the state belonging to its superclasses or subclasses.
* State is saved by writing the individual fields to the
* ObjectOutputStream using the writeObject method or by using the
* methods for primitive data types supported by DataOutput.
* readObject方法负责从流中读取并还原类字段。它可以调用in.defaultreadobject来调用默认机制来恢复对象的非静态和非瞬新
* 态字段。DefaultReadObject方法使用流中的信息为保存在流中的对象的字段分配当前对象中相应命名的字段。当类发展为添加
* 字段时,这将处理这种情况。该方法不需要关注属于它的超类或子类的状态。通过使用WriteObject方法将单个字段写入
* ObjectOutputStream或使用DataOutput支持的基元数据类型的方法来保存状态。
*
*
The readObjectNoData method is responsible for initializing the state of
* the object for its particular class in the event that the serialization
* stream does not list the given class as a superclass of the object being
* deserialized. This may occur in cases where the receiving party uses a
* different version of the deserialized instance's class than the sending
* party, and the receiver's version extends classes that are not extended by
* the sender's version. This may also occur if the serialization stream has
* been tampered; hence, readObjectNoData is useful for initializing
* deserialized objects properly despite a "hostile" or incomplete source
* stream.
* 如果序列化流未将给定类作为要反序列化的对象的超类列出,则ReadObjectNodeata方法负责为其特定类初始化对象的状态。
* 在接收方使用反序列化实例类的不同版本而不是发送方的情况下,可能会发生这种情况,并且接收方的版本扩展了不由发送方
* 版本扩展的类。如果序列化流已被篡改,也可能发生这种情况;因此,尽管源流“敌对”或不完整,但readObjectNoData对于正
* 确初始化反序列化对象非常有用。
* 假设当前有jvm中有两个SerialVersionUID一样的类(A、B)
* 1.A类序列化之后,B类添加新的属性,使用B类在反序列化对象时,新的属性采用类定义的默认值。
* 2.A类序列化之后,B类减少属性,使用B类反序列化对象时,减少的属性无法被读取。
* 3.A类在添加新的属性之后序列化,B类在反序列化对象时,A类中新添加的属性在B中无法读取。
*
*
Serializable classes that need to designate an alternative object to be
* used when writing an object to the stream should implement this
* special method with the exact signature:
*
*
* ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
*
*
* This writeReplace method is invoked by serialization if the method
* exists and it would be accessible from a method defined within the
* class of the object being serialized. Thus, the method can have private,
* protected and package-private access. Subclass access to this method
* follows java accessibility rules.
* 如果存在此方法,则通过序列化调用此WriteReplace方法,并且可以从被序列化对象的类中定义的方法访问它。因此,该方
* 法可以具有私有、受保护和包私有访问。这种方法的子类访问遵循Java可访问性规则。
* 当继承了Serializable接口的类中实现了writeReplace方法是,在进行序列化的时候会使用该方法代替writeObject实现序列化
* 这里的底层实现在研读过后续源码后在进行补充!
*
* Classes that need to designate a replacement when an instance of it
* is read from the stream should implement this special method with the
* exact signature.
* 当从流中读取替换实例时,需要指定替换的类应使用准确的签名实现此特殊方法。
*
*
* ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
*
*
* This readResolve method follows the same invocation rules and
* accessibility rules as writeReplace.
* 此readresolve方法遵循与writereplace相同的调用规则和可访问性规则。
*
* The serialization runtime associates with each serializable class a version
* number, called a serialVersionUID, which is used during deserialization to
* verify that the sender and receiver of a serialized object have loaded
* classes for that object that are compatible with respect to serialization.
* If the receiver has loaded a class for the object that has a different
* serialVersionUID than that of the corresponding sender's class, then
* deserialization will result in an {@link InvalidClassException}. A
* serializable class can declare its own serialVersionUID explicitly by
* declaring a field named "serialVersionUID"
that must be static,
* final, and of type long
:
* 序列化运行时与每个可序列化的类一个版本号(称为serialversionID)关联,该版本号在反序列化过程中用于验证序列化对象
* 的发送方和接收方是否已为该对象加载了与序列化兼容的类。如果接收者为与对应发送者类的serialversionID不同的对象加载
* 了一个类,那么反序列化将导致@link invalidclassexception。可序列化类可以通过声明一个名为
* “serialVersionUID”
的字段显式声明其自己的serialversionuid, 必须是static final long型。
*
*
* ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
*
InvalidClassException
s during deserialization. Therefore, toprivate
modifier whereinvalidClassException
。因此,为了保证在不同Java编译器实现之间实现一致的序列化版本值,private->code>修饰符,
* 因为此类声明仅适用于立即声明的类--serialversionuid字段作为继承成员不有用。数组类不能声明显式的serialversionID,
* 因此它们始终具有默认的计算值,但数组类不需要匹配serialversionID值。
*
* @author unascribed
* @see java.io.ObjectOutputStream
* @see java.io.ObjectInputStream
* @see java.io.ObjectOutput
* @see java.io.ObjectInput
* @see java.io.Externalizable
* @since JDK1.1
*/
public interface Serializable {
}