首页 > 其他 > 详细

认识 transient !

时间:2014-02-15 07:42:23      阅读:254      评论:0      收藏:0      [点我收藏+]

作用:

transient关键字修改的变量不会被序列化!


package discovery.transientTest;
import java.io.Serializable;
public class Transient implements Serializable{
    private static final long serialVersionUID = 1L;
    private transient int i = 0;
    private int j = 0;
    public Transient(int i,int j){
        this.i = i;
        this.j = j;
    }
    public String toString(){
        return "i = " + i + ", j = " + j;
    }
}

package discovery.transientTest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
 * 被transient修饰的变量不能被序列化
 */

public class Client {
    public static void main(String[] args) throws Exception {
        Client tt = new Client();
        tt.test();
    }
    public void test() throws Exception {
        Transient ti = new Transient(45); 
        System.out.println(ti.toString()); 
        ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("D:/ti.out"));
        o.writeObject(ti); 
        o.close(); 
        ObjectInputStream in =new ObjectInputStream(new FileInputStream("D:/ti.out")); 
        Transient logInfo = (Transient)in.readObject(); 
        System.out.println(logInfo.toString()); 
    }
}

输出:
= 4, j = 5
= 0, j = 5






认识 transient !

原文:http://www.cnblogs.com/java-z/p/3550125.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!