今天工做時發現一個序列化的問題:java
- public abstract class FirstLevel {//基類
- private Map map;
- public FirstLevel(){
- System.out.println("first");
- }
- protected void init(){
- map = new HashMap(0);
- }
- public Map getMap() {
- return map;
- }
- public void setMap(Map map) {
- this.map = map;
- }
- }
- public class SecondLevel extends FirstLevel implements Serializable{
- public SecondLevel(){
- init();
- System.out.println("second");
- }
- }
- public class Test {
- public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException{
- System.out.println("before:");
- SecondLevel before = new SecondLevel();
- HashMap a = new HashMap();
- a.put("key", "guojje");
- before.setMap(a);
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:\\t.txt"));
- oos.writeObject(before);
- oos.close();
- System.out.println("after:");
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c:\\t.txt"));
- SecondLevel after = (SecondLevel)ois.readObject();
- ois.close();
- System.out.println(after.getMap());
- }
- }