SCJP題庫第148題


Given:
12. import java.io.*;
13. public class Forest implements Serializable{
14.   private Tree tree = new Tree();
15.   public static void main(String[] args){
16.     Forest f = new Forest();
17.   try{
18.   FileOutputStream fs = new FileOutputStream("Forest.ser");
19.   ObjectOutputStream os = new ObjectOutputStream(fs);
20.   os.writeObject(f); os.close();
21.   }catch(Exception ex){ex.printStackTrace();}
22. }}
23.
24. class Tree{}
What is the result?
 
 
A. Compilation fails.
B. An exception is thrown at runtime.
C. An instance of Forest is serialized.
D. An instance of Forest and an instance of Tree are both serialized.
 
Ans: B
 
解說:
Forest物件所擁有的物件Tree也同樣要宣告實作Serializable,這樣整個包含關係的物件才能正確使Javaserializable機制
24. class Tree implements Serializable{}

Comments

Popular posts from this blog

Android-使用webview在V3版的Google地圖GPS定位