Serializable 직렬 화 분석

4166 단어 xml
1、  Serializable      API    ,    package-private
    private                     
2、Serializable            ,       、       、
   public protected  ,               publish API,
             long     serialVersionUID field,      
     getXX,                      (           
      ?)
3、       Serializable       ,       ,    
   extralinguistic mechanism,     Serializable           
              
4
5、1.4   ,JavaBeans        XML   ,    Serializable
6、          ,     Serializable,      interface
        Serializable。         Serializable  ,       ,
                             。  ,        
   Serializable                           .
7、         Serializable  ,   static ,(          
          )
8
   :



public class StringList implements Serializable
{
private int size = 0;
private Entry head = null;

private static class Entry implements Serializable
{
  String data;
   Entry next;
   Entry previous;
}
...//Remainder ommitted
}
( )
, :
//String List with a resonable custom serialized form

class StringList implements Serializable
{
  private transient int size = 0;        //!transient
  private transient Entry head = null;   //!transient
  
   //no longer serializable!
  private static class Entry
   {
     String data;
     Entry next;
     Entry previous;
   }
  
   //Appends the specified string to the list
  public void add(String s) {/*...*/};
  
   /**
    * Serialize this StringList instance
    * @author yuchifang
    * @serialData The size of the list (the number of strings
    * it contains) is emitted(int), in the proper sequence
    */
  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);
   }
  
  private void readObject(ObjectInputStream s)
               throws IOException, ClassNotFoundException
   {
    int numElements = s.readInt();
    
     //Read in all elements andd insert them in list
    for (int i = 0; i < numElements; i++)
       add((String)s.readObject());
   }
   //...remainder omitted
}
   UID:private static final long serialVersionUID = randomLongValue;
10、           transient    ,      。
11、writeObject/readObject           readResolve
     writeReplace          invariant controllers

9. 당신 이 어떤 직렬 화 형식 을 선택 하 든 명시 적 인 것 을 성명 합 니 다.

좋은 웹페이지 즐겨찾기