supernotserial.java
来自「SCJP认证考试资料」· Java 代码 · 共 37 行
JAVA
37 行
import java.io.*;
class SuperNotSerial {
public static void main(String [] args) {
Dog d = new Dog(35, "Fido");
System.out.println("before: " + d.name + " "
+ d.weight);
try {
FileOutputStream fs = new FileOutputStream("testSer.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(d);
os.close ();
} catch (Exception e) { e.printStackTrace(); }
try {
FileInputStream fis = new FileInputSream("testSer.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
d = (Dog) ois.readObject();
ois.close();
} catch (Exception e) { e.printStackTrace(); }
System.out.println("after: " + d.name + " "
+ d.weight);
}
}
class Dog extends Animal implements Serializable {
String name;
Dog(int w, String n) {
weight = w; // inherited
name = n; // not inherited
}
}
class Animal { // not serializable !
int weight = 42;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?