Java中的transient关键字
2017-08-19 21:08
423 查看
转载于:[lfsf802](http://blog.csdn.net/lfsf802/article/details/43239663)
关键字介绍 一个对象只要实现了Serilizable接口,这个对象就可以被序列化,Java的这种序列化模式为开发者提供了很多便利,可以不必关系具体序列化的过程,只要这个类实现了Serilizable接口,这个的所有属性和方法都会自动序列化。但是有种情况是有些属性是不需要序列号的,所以就用到这个关键字。只需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中。 代码: 实现Serializable接口的UserInfo类,并且有一个transient关键字修饰的属性 [java] view plain copy print?package com.testtransient.model;
import java.io.Serializable;
public class UserInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String name ;
private transient String pwd ;
public UserInfo(String name,String pwd){
this.name =name;
this.pwd =pwd;
}
public String toString(){
return “name=” +name +“,psw=” +pwd ;
}
}
通过输入输出流编写的测试程序
[java] view plain copy print?public class TestTransient {
public static void main(String[] args) {
UserInfouserInfo = new UserInfo(“张三” , “123456” );
System. out.println(userInfo);
try {
// 序列化,被设置为transient的属性没有被序列化
ObjectOutputStreamo = new ObjectOutputStream(new FileOutputStream(
”UserInfo.out”));
o.writeObject(userInfo);
o.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
// 重新读取内容
ObjectInputStream in = new ObjectInputStream( new FileInputStream(
”UserInfo.out”));
UserInforeadUserInfo = (UserInfo) in.readObject()
bf82
;
// 读取后psw的内容为null
System. out.println(readUserInfo.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
运行结果:
从上面结果能够看出来经过transient关键字修饰的字段是不能够被序列化的。
关键字介绍 一个对象只要实现了Serilizable接口,这个对象就可以被序列化,Java的这种序列化模式为开发者提供了很多便利,可以不必关系具体序列化的过程,只要这个类实现了Serilizable接口,这个的所有属性和方法都会自动序列化。但是有种情况是有些属性是不需要序列号的,所以就用到这个关键字。只需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中。 代码: 实现Serializable接口的UserInfo类,并且有一个transient关键字修饰的属性 [java] view plain copy print?package com.testtransient.model;
import java.io.Serializable;
public class UserInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String name ;
private transient String pwd ;
public UserInfo(String name,String pwd){
this.name =name;
this.pwd =pwd;
}
public String toString(){
return “name=” +name +“,psw=” +pwd ;
}
}
package com.testtransient.model; import java.io.Serializable; public class UserInfo implements Serializable { private static final long serialVersionUID = 1L; private String name ; private transient String pwd ; public UserInfo(String name,String pwd){ this.name =name; this.pwd =pwd; } public String toString(){ return "name=" +name +",psw=" +pwd ; } }
通过输入输出流编写的测试程序
[java] view plain copy print?public class TestTransient {
public static void main(String[] args) {
UserInfouserInfo = new UserInfo(“张三” , “123456” );
System. out.println(userInfo);
try {
// 序列化,被设置为transient的属性没有被序列化
ObjectOutputStreamo = new ObjectOutputStream(new FileOutputStream(
”UserInfo.out”));
o.writeObject(userInfo);
o.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
// 重新读取内容
ObjectInputStream in = new ObjectInputStream( new FileInputStream(
”UserInfo.out”));
UserInforeadUserInfo = (UserInfo) in.readObject()
bf82
;
// 读取后psw的内容为null
System. out.println(readUserInfo.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public class TestTransient { public static void main(String[] args) { UserInfouserInfo = new UserInfo("张三" , "123456" ); System. out.println(userInfo); try { // 序列化,被设置为transient的属性没有被序列化 ObjectOutputStreamo = new ObjectOutputStream(new FileOutputStream( "UserInfo.out")); o.writeObject(userInfo); o.close(); } catch (Exception e) { e.printStackTrace(); } try { // 重新读取内容 ObjectInputStream in = new ObjectInputStream( new FileInputStream( "UserInfo.out")); UserInforeadUserInfo = (UserInfo) in.readObject(); // 读取后psw的内容为null System. out.println(readUserInfo.toString()); } catch (Exception e) { e.printStackTrace(); } }
运行结果:
从上面结果能够看出来经过transient关键字修饰的字段是不能够被序列化的。
相关文章推荐
- Java中的transient,volatile和strictfp关键字
- Java基础-transient关键字
- java中的transient关键字作用
- Java transient关键字使用小记
- Java transient关键字使用小记
- java transient关键字
- java关键字Transient
- Java关键字之native,strictfp,transient,volatile
- Java中的关键字transient
- Java中的transient,volatile和strictfp关键字
- java transient关键字
- Java中的transient,volatile和strictfp关键字
- java中transient关键字
- Java transient关键字使用小记
- Java transient关键字
- Java你所不知道的细节——transient关键字(2)
- Java关键字native、volatile、transient
- java修饰符:transient关键字
- Java中的transient关键字
- java关键字transient