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. <p>

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

 

 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. <p>

翻译:遍历图的时候,如果不可序列化,则会抛出NotSerializableException

 

 Classes that require special handling during the serialization and
* deserialization process must implement special methods with these exact
* signatures:

翻译:如果需要特别处理序列化和反序列化过程,则要实现如下方法

<PRE>
* private void writeObject(java.io.ObjectOutputStream out)
*     throws IOException
* private void readObject(java.io.ObjectInputStream in)
*     throws IOException, ClassNotFoundException;
* private void readObjectNoData()
*     throws ObjectStreamException;
* </PRE>
 <p>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.

 

<p>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方法。。。

 

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值