Java scjp 反序列化問題

題目如下(節至SCJP超擬真試題): import java.io.*; class Food{ Food(){System.out.println("1")}; } class Fruit extends Food implements Serializable{ Fruit(){System.out.println("2")}; } public class Banana2 extends Fruit{ int size = 42; public static void main(String[]... 顯示更多 題目如下(節至SCJP超擬真試題):

import java.io.*;
class Food{
Food(){System.out.println("1")};
}
class Fruit extends Food implements Serializable{
Fruit(){System.out.println("2")};
}
public class Banana2 extends Fruit{
int size = 42;
public static void main(String[] args){
Banana2 b = new Banana2();
b.serializeBanana2(b);//assume correct serialize line 12
b=b.deserializeBanana2(b);//assume correct line 13
System.out.println(" restored " + b.size + " ");
}
}

程式碼如上,答案是:121 restored 42(我選12 restored 42)
問題來了,看了他的詳解我依然搞不懂
他說因為13行反序列化時JVM會建立Food的實體,而使答案多了1
可是為啥會建立其實體,請幫我解釋一下,謝謝。
1 個解答 1