首页 技术 正文
技术 2022年11月15日
0 收藏 533 点赞 5,071 浏览 2201 个字

转载于:[lfsf802](http://blog.csdn.net/lfsf802/article/details/43239663)

关键字介绍

一个对象只要实现了Serilizable接口,这个对象就可以被序列化,Java的这种序列化模式为开发者提供了很多便利,可以不必关系具体序列化的过程,只要这个类实现了Serilizable接口,这个的所有属性和方法都会自动序列化。但是有种情况是有些属性是不需要序列号的,所以就用到这个关键字。只需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中。

代码:

实现Serializable接口的UserInfo类,并且有一个transient关键字修饰的属性

  1. package com.testtransient.model;
  2. import java.io.Serializable;
  3. public class UserInfo implements Serializable {
  4. private static final long serialVersionUID = 1L;
  5. private String name ;
  6. private transient String pwd ;
  7. public UserInfo(String name,String pwd){
  8. this.name =name;
  9. this.pwd =pwd;
  10. }
  11. public String toString(){
  12. return “name=” +name +“,psw=” +pwd ;
  13. }
  14. }
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 ;
}
}

通过输入输出流编写的测试程序

  1. public class TestTransient {
  2. public static void main(String[] args) {
  3. UserInfouserInfo = new UserInfo(“张三” , “123456” );
  4. System. out.println(userInfo);
  5. try {
  6. // 序列化,被设置为transient的属性没有被序列化
  7. ObjectOutputStreamo = new ObjectOutputStream(new FileOutputStream(
  8. ”UserInfo.out”));
  9. o.writeObject(userInfo);
  10. o.close();
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }
  14. try {
  15. // 重新读取内容
  16. ObjectInputStream in = new ObjectInputStream( new FileInputStream(
  17. ”UserInfo.out”));
  18. UserInforeadUserInfo = (UserInfo) in.readObject();
  19. // 读取后psw的内容为null
  20. System. out.println(readUserInfo.toString());
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
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关键字修饰的字段是不能够被序列化的。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,136
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,300