zoukankan      html  css  js  c++  java
  • transient关键字的应用——《Thinking in Java》随笔032

    使用transient方式修饰过的成员属性不会被写入流中,

    就不用实现Externalizable接口来逐个选择成员属性。

    看下面的例子(摘自Thinking in Java):

      1 //: Logon.java
      2 package c10;
      3 
      4 import java.io.FileInputStream;
      5 import java.io.FileOutputStream;
      6 import java.io.ObjectInputStream;
      7 import java.io.ObjectOutputStream;
      8 import java.io.Serializable;
      9 import java.util.Date;
     10 
     11 /**
     12 *    @time:         下午4:37:21
     13 *    @date:         2017年5月8日
     14 *    @auther:    skyfffire
     15 *    @version:    v0.1
     16 */
     17 public class Logon implements Serializable {
     18     private static final long serialVersionUID = -1348372416300355940L;
     19     
     20     private Date date = new Date();
     21     private String username;
     22     private transient String password;
     23     
     24     Logon(String _username, String _password) {
     25         username = _username;
     26         password = _password;
     27     }
     28     
     29     /**
     30      * @return the date
     31      */
     32     public Date getDate() {
     33         return date;
     34     }
     35 
     36     /**
     37      * @param date the date to set
     38      */
     39     public void setDate(Date date) {
     40         this.date = date;
     41     }
     42     /**
     43      * @return the username
     44      */
     45     public String getUsername() {
     46         return username;
     47     }
     48     /**
     49      * @param username the username to set
     50      */
     51     public void setUsername(String username) {
     52         this.username = username;
     53     }
     54     /**
     55      * @return the password
     56      */
     57     public String getPassword() {
     58         return password;
     59     }
     60     /**
     61      * @param password the password to set
     62      */
     63     public void setPassword(String password) {
     64         this.password = password;
     65     }
     66     /**
     67      * @return the serialversionuid
     68      */
     69     public static long getSerialversionuid() {
     70         return serialVersionUID;
     71     }
     72 
     73     /* (non-Javadoc)
     74      * @see java.lang.Object#toString()
     75      */
     76     @Override
     77     public String toString() {
     78         return "Logon [date=" + date + 
     79                 ", username=" + username + 
     80                 ", password=" + password + "]";
     81     }
     82     
     83     public static void main(String[] args) {
     84         Logon a = new Logon("Hulk", "Haha");
     85         
     86         System.out.println("Logon a = " + a);
     87         
     88         try {
     89             ObjectOutputStream out = 
     90                     new ObjectOutputStream(
     91                             new FileOutputStream("Logon.out"));
     92             out.writeObject(a);
     93             out.close();
     94             
     95             // 1.线程式暂停
     96             try {
     97                 Thread.sleep(2000);
     98             } catch (Exception e) {
     99                 e.printStackTrace();
    100             }
    101 
    102             // 2.循环式暂停
    103             int seconds = 2;
    104             // currentTimeMillis()获取当前系统时间
    105             long t = System.currentTimeMillis() + seconds * 1000;
    106             while (System.currentTimeMillis() < t);
    107             
    108             ObjectInputStream in = 
    109                     new ObjectInputStream(new FileInputStream("Logon.out"));
    110             Logon b = (Logon) in.readObject();
    111             System.out.println("Logon b = " + b);
    112             in.close();
    113         } catch (Exception e) {
    114             e.printStackTrace();
    115         }
    116     }
    117 }
    118 
    119 ///:~
  • 相关阅读:
    设置系统时间
    设置访问URL不要项目名二级目录
    tomcat部署项目报错NoSuchMethodException#addServlet,addFilter
    Java多线程-线程池ThreadPoolExecutor构造方法和规则
    IDEA Debug 快捷键
    收藏网站
    eclipse添加hadoop插件
    Frida Mac环境搭建
    Android UiAutomator 1&2
    Android下monkey命令参数
  • 原文地址:https://www.cnblogs.com/skyfffire/p/6825918.html
Copyright © 2011-2022 走看看