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,這樣整個包含關係的物件才能正確使用Java的serializable機制
24. class Tree implements Serializable{}
Comments