群晖网站建设处理错误500,凡科网站内容怎么做效果好,行业门户网站建设费用,东莞易宣网站建设公司怎么样在Java中#xff0c; transient字段在序列化过程中被排除。 简而言之#xff0c;当我们将对象保存到文件中#xff08;序列化#xff09;时#xff0c;所有transient字段都将被忽略。
1. POJO 瞬态
复查以下Person类#xff1b; 薪水领域是transient 。
public class …在Java中 transient字段在序列化过程中被排除。 简而言之当我们将对象保存到文件中序列化时所有transient字段都将被忽略。
1. POJO 瞬态
复查以下Person类 薪水领域是transient 。
public class Person implements Serializable {private static final long serialVersionUID 1L;private String name;private int age;// ignore this fieldprivate transient BigDecimal salary;//...
}2.序列化
2.1在序列化期间瞬态现场salary将不包括在内。
ObjectUtils.java
package com.mkyong.io.object;import java.io.*;
import java.math.BigDecimal;public class ObjectUtils {public static void main(String[] args) throws IOException, ClassNotFoundException {Person person new Person(mkyong, 40, new BigDecimal(900));// object - filetry (FileOutputStream fos new FileOutputStream(person.obj);ObjectOutputStream oos new ObjectOutputStream(fos)) {oos.writeObject(person);oos.flush();}Person result null;// file - objecttry (FileInputStream fis new FileInputStream(person.obj);ObjectInputStream ois new ObjectInputStream(fis)) {result (Person) ois.readObject();}System.out.println(result);}}2.2现在我们删除了transient关键字。
Person.java
public class Person implements Serializable {private static final long serialVersionUID 1L;private String name;private int age;private BigDecimal salary;//...
}重新运行它这次将显示薪水字段。
Terminal
Person{namemkyong, age40, salary900}