,我讀了「序列化不是靜態元素」 - 但低於一個簡單的例子告訴我,否則。靜態成員系列化
class superparent implements Serializable {
int data = 0;
public superparent(int data) {
this.data = data;
}
public int getdata() {
return data;
}
}
public class statichost implements Serializable {
int member = 0;
public static superparent s = new superparent(20);
public statichost(int data) {
this.member = data;
}
public static void main(String[] args) {
statichost c = new statichost(6);
try {
FileOutputStream fs = new FileOutputStream("testSer.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(c);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
FileInputStream fis = new FileInputStream("testSer.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
c = (statichost) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("after: contained data is " + c.s.getdata());
}
}
當我按照上面的說法期望0時,輸出打印20。 我錯過了什麼嗎?
恭喜OP接受你的答案,這是我自己的單詞副本的近乎單詞! – Perception
看起來你的一個更通用,更好,但我沒有讀你回答我發佈我的。對不起,沒有人想複製其他人的愚蠢答案... – Kleenestar