📄 student.java
字号:
package Day25;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student implements Serializable{//erializable是空接口 需要实现方法
String name;
String sex;
int age;
static String school = null;//这个不会被序列化,
transient float weight = 0;//这个也不会被序列化,保存也不会保存,恢复出来的都是默认值
public Student(String name,String sex,int age){
this.setName(name);
this.setSex(sex);
this.setAge(age);
}
public void setName(String n){
this.name = n;
}
public String getName(){
return name;
}
public void setSex(String s){
this.sex = s;
}
public String getSex(){
return sex;
}
public void setAge(int a){
this.age = a;
}
public int getAge(){
return age;
}
public static void test3() throws Exception{
Student s = new Student("王八","母",100);
//以下二个不会被序列化,也保存不了,恢复得到默认值
s.school = "xdf";
s.weight = 88;
//以下保存对象 序列化 只序列化常员变量和类型,不会序列化方法、静态的属性 还有transient修饰的东西
ObjectOutputStream out = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream("c:\\test.dat",true)));
out.writeObject(s);
out.flush();
out.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -