SCJP 1.6版考題 146

出自 陳富國維基館
於 2013年3月30日 (六) 03:05 由 Ikk (對話 | 貢獻) 所做的修訂 (新頁面: Given: 1. import java.io.*; 2. public class Foo implements Serializable{ 3. public int x, y; 4. public Foo(int x, int y){this.x = x; this.y = y;} 5. 6. private void writeO...)
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋
Given:
1. import java.io.*;
2. public class Foo implements Serializable{
3.   public int x, y;
4.   public Foo(int x, int y){this.x = x; this.y = y;}
5.
6.   private void writeObject(ObjctOutputStream s)
7.   throws IOException{
8.     s.writeInt(x); s.writeInt(y);
9.   }
10.
11. private void readObject(ObjectInputStream s)
12. throws IOException, ClassNotFoundException{
13.
14.   //insert code here
15.
16. }
17.}

Which code, inserted at line 14, will allow this class to correctly serialize and deserialize?
 A. s.defaultReadObject();
 B. this = s.defaultReadObject(),
 C. y = s.readInt(); x = s.readInt();
 D. x = s.readInt(); y = s.readInt();

解答


Ans: D

解說: 寫入串流與讀出串流的順序要一致