天天看點

序列化繼承(轉)

public class SerializableEr implements Serializable{  


    /** 

     *  

    */  

    private static final long serialVersionUID = 1L;  

     

}  


public class Serial extends SerializableEr {  

    int id;  

    String name;  

    public Serial(int id, String name) {  

        this.id = id;  

        this.name = name;  

    }  

    public String toString() {  

        return "DATA: " + id + " " +name;  

  

    }  

    public static void main(String[] args) {  

        Serial serial=new Serial(1,"4234");  

        System.out.println("object serial:"+serial);   

        try{  

            FileOutputStream fos=new FileOutputStream("serialTest.txt");   

           ObjectOutputStream oos=new ObjectOutputStream(fos);   

            oos.writeObject(serial);   

            oos.flush();   

            oos.close();   

        }catch(Exception e){  

            System.out.println("Exception:"+e);   

        }  

    }  

}  


為一個實作Serializable接口的父類,編寫一個能夠序列化的子類 子類将自動的實作序列化