我有以下代码:
import java.io.*;
public class TestSer {
public static void main(String[] args) {
SpecialSerial s = new SpecialSerial();
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myFile"));
os.writeObject(s); os.close();
System.out.print(++s.z + " "); ...(1)
ObjectInputStream is = new ObjectInputStream(new FileInputStream("myFile"));
SpecialSerial s2 = (SpecialSerial)is.readObject();
is.close();
System.out.println(s2.y + " " + s2.z);
} catch (Exception x) {
System.out.println("exc");
}
}
}
class SpecialSerial implements Serializable {
transient int y = 7;
static int z = 9;
}
现在,当我运行代码时,输出为“10 0 10”。但为什么是“10 0 10”而不是“10 0 0”?我的意思是当我反序列化对象时,y 和 z(分别是瞬态和静态)应该返回为 7 和 9,它们是默认值(请在这一点上纠正我,因为我认为对象在反序列化后获得的值是默认值)。语法“++object.var”是什么意思(参考(1))。是否与“Object var++”相同,即“++sz”是否与“s.z++”相同?