posts - 241,  comments - 116,  trackbacks - 0
          transient說明一個屬性是臨時的,不會被序列化。
          下面是一個Demo,name聲明為 transient,不被序列化
          package com.zzs.tet;

          import java.io.File;
          import java.io.FileInputStream;JDK源代碼之LinkedList類分析
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.ObjectInput;
          import java.io.ObjectInputStream;
          import java.io.ObjectOutput;
          import java.io.ObjectOutputStream;
          import java.io.Serializable;

          public class TransientDemo implements Serializable{
              /**
               *
               */
              private static final long serialVersionUID = 1L;
              private  transient String name;
              private String password;
              
              public String getName() {
                  return name;
              }

              public void setName(String name) {
                  this.name = name;
              }

              public String getPassword() {
                  return password;
              }

              public void setPassword(String password) {
                  this.password = password;
              }

              /**
               * @param args
               * @throws IOException
               * @throws FileNotFoundException
               * @throws ClassNotFoundException
               */
              public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
                  // TODO Auto-generated method stub
                  String path="D:"+File.separator+"object.txt";
                  File file=new File(path);
                  TransientDemo transientDemo=new TransientDemo();
                  transientDemo.setName("姓名");
                  transientDemo.setPassword("密碼");
                  ObjectOutput output=new ObjectOutputStream(new FileOutputStream(file));
                  output.writeObject(transientDemo);
                  ObjectInput input=new ObjectInputStream(new FileInputStream(file));
                  TransientDemo demo=(    TransientDemo )input.readObject();
                  System.out.println(demo.getName()+demo.getPassword());
              }

          }
          posted on 2011-07-26 09:29 墻頭草 閱讀(600) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 汉寿县| 板桥市| 察雅县| 神池县| 新平| 灌阳县| 武清区| 佛山市| 华池县| 台安县| 孟津县| 石阡县| 合江县| 宁南县| 施秉县| 巫溪县| 昭平县| 会理县| 利津县| 德清县| 五台县| 富民县| 临清市| 页游| 隆安县| 寿阳县| 大新县| 安平县| 云浮市| 温泉县| 罗源县| 临沧市| 英超| 栖霞市| 龙陵县| 白河县| 洛浦县| 大连市| 葫芦岛市| 铜陵市| 龙泉市|