s-day10-static和transient在序列化中的使用
This commit is contained in:
@@ -15,7 +15,7 @@ import java.io.ObjectOutputStream;
|
||||
*/
|
||||
public class Demo09 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Student student = new Student("张三", 18);
|
||||
Student student = new Student("张三", 18,"123456");
|
||||
//获取序列化流对象,写出对象(将对象保存到文件中)
|
||||
FileOutputStream fos = new FileOutputStream("student.txt");
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
||||
|
||||
@@ -5,12 +5,27 @@ import java.io.Serializable;
|
||||
public class Student implements Serializable {
|
||||
String name;
|
||||
int age;
|
||||
/*
|
||||
注意:
|
||||
1.静态内容只跟类有关,跟对象无关,所以序列化时静态内容不能被保存的
|
||||
2.transient表示瞬态,瞬间的值,被它修饰的成员变量,就不能序列化
|
||||
*/
|
||||
|
||||
//static String password;//针对静态数据不能保存下来,但是会被共享,不符合业务
|
||||
transient String password;//瞬态,该变量就不会被序列化
|
||||
|
||||
public Student(String name, int age, String password) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student{" +
|
||||
"name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
", password='" + password + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user