复习transient关键字的使用

一句话概括:对于某些数据,如果你不想serialize,那么就让之transient,最简单的例子——password,代码如下:

 1 class LoggingInfo implements java.io.Serializable   
 2 {   
 3     private Date loggingDate = new Date();   
 4     private String uid;   
 5     private transient String pwd;  
 6       
 7     LoggingInfo(String user, String password)   
 8     {   
 9         uid = user;   
10         pwd = password;   
11     }   
12     public String toString()   
13     {   
14         String password=null;   
15         if(pwd == null)   
16         {   
17         password = "NOT SET";   
18         }   
19         else  
20         {   
21             password = pwd;   
22         }   
23         return "logon info: \n   " + "user: " + uid +   
24             "\n   logging date : " + loggingDate.toString() +   
25             "\n   password: " + password;   
26     }
27     
28 }  
29 
30 class GuestLoggingInfo extends LoggingInfo {
31     
32     public GuestLoggingInfo() {
33         super("guest", "guest");
34     }
35     public GuestLoggingInfo(String user, String password) {
36         super(user, password);
37     }
38 }
39 
40 public class Foo {
41     
42     public static void main(String[] args) {
43         LoggingInfo logInfo = new LoggingInfo("MIKE", "MECHANICS");   
44         System.out.println(logInfo.toString());   
45         try  
46         {   
47            ObjectOutputStream o = new ObjectOutputStream(   
48                         new FileOutputStream("e:/logInfo.out"));   
49            o.writeObject(logInfo);   
50            o.close();   
51         }   
52         catch(Exception e) {
53             //deal with exception
54         }   
55           
56         try  
57         {   
58            ObjectInputStream in =new ObjectInputStream(   
59                         new FileInputStream("e:/logInfo.out"));   
60            LoggingInfo logInfo2 = (LoggingInfo)in.readObject();   
61            System.out.println(logInfo2.toString());   
62         }   
63         catch(Exception e) {
64             //deal with exception
65         }
66         
67         System.out.println("---------------------");
68         
69         GuestLoggingInfo guestLoggingInfo = new GuestLoggingInfo();
70         System.out.println(guestLoggingInfo);
71         try {
72             ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("e:/logInfo.out"));
73             objectOutputStream.writeObject(guestLoggingInfo);
74             objectOutputStream.close();
75         } catch (Exception e) {
76             // TODO: handle exception
77         }
78         try {
79             ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("e:/logInfo.out"));
80             guestLoggingInfo = (GuestLoggingInfo)objectInputStream.readObject();
81             objectInputStream.close();
82         } catch (Exception e) {
83             // TODO: handle exception
84         }
85         System.out.println(guestLoggingInfo); // constructor并没有被调用
86     }
87 }

 

 

posted @ 2013-06-14 22:08  rldts  阅读(176)  评论(0编辑  收藏  举报