Java关键字transient

使用transient和不使用transient的结果对比:
Person类:

package cn.ancony.useful;

import java.io.Serializable;

/**
 * Created by Ancony.
 */
public class Person implements Serializable {

    private transient String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

测试类:

package cn.ancony.useful;

import java.io.*;

/**
 * Created by Ancony.
 */
public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Person outPerson = new Person("Ancony", 18);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("person")));
        oos.writeObject(outPerson);
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("person")));
        Person inPerson = (Person) ois.readObject();
        System.out.println(inPerson.getName());
        System.out.println(inPerson.getAge());
    }
}

输出结果:

null
18

以上的测试表明,如果一个实现了Serializable接口的类的字段使用了transient来修饰,那么该字段不会参与序列化。以上的例子是针对实例变量,对于静态变量,由于静态变量是归类所有。所以,获取到的是JVM中类的变量,而不是从文件中反序列化得到的值。

对于有继承关系的类,如果父类实现了Serializable接口,子类自然也就实现了该接口,如果父类没有实现Serializable接口,而子类实现了该接口时,子类又没有隐藏父类中的变量时,子类中tranient标记的变量不会序列化,没有标记的会序列化。而子类继承的父类的变量,即使在子类中重新赋值,反序列化后得到的是父类中的值,而不是子类中新值。

下面是个小例子:

父类Person:

package cn.ancony.useful;

import java.io.Serializable;

/**
 * Created by Ancony.
 */
public class Person {

    protected String name;
    protected int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

}

子类Student:

package cn.ancony.useful;

import java.io.Serializable;

/**
 * Created by Ancony.
 */
public class Student extends Person implements Serializable {

    private transient String grade;
    private String classRoom;

    public Student() {
        super("Ancony", 18);
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getClassRoom() {
        return classRoom;
    }

    public void setClassRoom(String classRoom) {
        this.classRoom = classRoom;
    }
}

测试类:

package cn.ancony.useful;

import java.io.*;

/**
 * Created by Ancony.
 */
public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student outStudent= new Student();
        outStudent.setGrade("senior");
        outStudent.setClassRoom("1001");
        outStudent.name="Conan";
        outStudent.age=19;
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("student")));
        oos.writeObject(outStudent);

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("student")));
        Student inStudent = (Student) ois.readObject();
        System.out.println(inStudent.name);
        System.out.println(inStudent.age);
        System.out.println(inStudent.getGrade());
        System.out.println(inStudent.getClassRoom());
    }
}

测试结果:

null
0
null
1001
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值