Java序列化与Serializable(五)readObjectNoData方法

 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. 

翻译:没有序列化起码要有无参构造。

 

 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.

 

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.

-----------------------------------------

这两个方法前面已经用过

下面看下本篇的重点 readObjectNoData

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

重点语句:the serialization * stream does not list the given class as a superclass of the object being * deserialized.

测试一下:

初始序列化状态:

public class TestBean  implements Serializable {
    public String property1;
    public String property2;
    public int property3;
    public String desc;
    public static final int serialVersionUID = 1;
    @Override
    public String toString() {
        return "TestBean{" +
                "desc='" + desc + '\'' +
                ", property1='" + property1 + '\'' +
                ", property2='" + property2 + '\'' +
                ", property3=" + property3 +
                '}'+super.toString();
    }
}

将这个类序列化之后,修改一下这个类,继承baseBean

public class TestBean extends BaseBean implements Serializable {
    public String property1;
    public String property2;
    public int property3;
    public String desc;
    public static final int serialVersionUID = 1;
    @Override
    public String toString() {
        return "TestBean{" +
                "desc='" + desc + '\'' +
                ", property1='" + property1 + '\'' +
                ", property2='" + property2 + '\'' +
                ", property3=" + property3 +
                '}'+super.toString();
    }
}

baseBean 如下

public class BaseBean  implements Serializable {
   private static final String TAG = "TestService";
   public String property4;
   public String property5;
   public int property6;

   public BaseBean(String id){

   }

   public BaseBean(){}
   //setter getter...
   private void readObjectNoData() {
      Log.i(TAG,"readObjectNoData");
      this.property4 = "readObject ....";
      this.property5 = "readObject ....";
      this.property6 = 0;
   }

   @Override
   public String toString() {
      return "BaseBean{" +
              "property4='" + property4 + '\'' +
              ", property5='" + property5 + '\'' +
              ", property6=" + property6 +
              '}';
   }
}

此时会调用父类的 readObjectNoData方法。。。

 

说明:感觉这个方法用的地方不多。。

你可能感兴趣的:(安卓)