シーケンス化とtransientキーワードについて

5210 ワード

まず例を見てみましょう.
demo1
package com.caiya.test._transient;

import java.io.Serializable;

/**
 * Created by caiya on 16/3/31.
 */
public class User implements Serializable {

    private static final long serialVersionUID = -5627047021580508291L;

    private Long userId;

    public static String userName;

    private transient String password;


    public User() {

    }

    public User(Long userId, String userName, String password) {
        this.userId = userId;
        this.userName = userName;
        this.password = password;
    }

    public Long getUserId() {
        return userId;
    }

    public User setUserId(Long userId) {
        this.userId = userId;
        return this;
    }

    public String getPassword() {
        return password;
    }

    public String getUserName() {
        return userName;
    }

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

    public User setUserName(String userName) {
        this.userName = userName;
        return this;
    }
}
package com.caiya.test._transient;

import com.alibaba.fastjson.JSON;

import java.io.*;

/**
 * Created by caiya on 16/3/31.
 */
public class TransientTest {


    public static void main(String[] args) throws IOException, ClassNotFoundException {
        TransientTest transientTest = new TransientTest();
        transientTest.test1();
        User.userName = "lisi";
        transientTest.test2();
    }

    public void test1() throws IOException {
        User user = new User(1L, "zhangsan", "111111");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("/Users/caiya/workspace/test/transient.txt")));
        objectOutputStream.writeObject(user);
        objectOutputStream.flush();
        objectOutputStream.close();
    }

    public void test2() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("/Users/caiya/workspace/test/transient.txt")));
        Object object = objectInputStream.readObject();
        System.out.print(JSON.toJSONString(object));
    }


}

出力結果:
{"userId":1,"userName":"lisi"}

demo2
package com.caiya.test._transient;

import java.io.*;

/**
 * Created by caiya on 16/3/31.
 */
public class User2 implements Externalizable {

    private static final long serialVersionUID = 127908457926642843L;

    private Long userId;

    public static String userName = "default";

    private transient String password;


    public User2() {

    }

    public User2(Long userId, String userName, String password) {
        this.userId = userId;
        this.userName = userName;
        this.password = password;
    }

    public Long getUserId() {
        return userId;
    }

    public User2 setUserId(Long userId) {
        this.userId = userId;
        return this;
    }

    public String getPassword() {
        return password;
    }

    public String getUserName() {
        return userName;
    }

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

    public User2 setUserName(String userName) {
        this.userName = userName;
        return this;
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(this.getUserName());
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        userName = (String) in.readObject();
    }
}
package com.caiya.test._transient;

import com.alibaba.fastjson.JSON;

import java.io.*;

/**
 * Created by caiya on 16/3/31.
 */
public class TransientTest {


    public static void main(String[] args) throws IOException, ClassNotFoundException {
        TransientTest transientTest = new TransientTest();
        transientTest.test1();
        User2.userName = "lisi";
        transientTest.test2();
    }

    public void test1() throws IOException {
        User2 user = new User2(1L, "zhangsan", "111111");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("/Users/caiya/workspace/test/transient.txt")));
        objectOutputStream.writeObject(user);
        objectOutputStream.flush();
        objectOutputStream.close();
    }

    public void test2() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("/Users/caiya/workspace/test/transient.txt")));
        Object object = objectInputStream.readObject();
        System.out.print(JSON.toJSONString(object));
    }


}

出力結果:
{"userName":"zhangsan"}

まとめ
1、javaオブジェクトのシーケンス化はjavaを実現することができる.io.Serializableまたはjava.io.Externalizableインタフェース;
2、transient修飾を用いた変数はシーケンス化できない.
3、staticキーを使用するデフォルトはメモリに保存され、シーケンス化が必要な場合はjavaを書き換えることができる.io.ExternalizableのwriteExternal/readExternalメソッド