因为你有标志这个class是可序列化的(implements Serializable)Object***Stream会自己通过反射机理去调用的。 /** * Reads (or attempts to skip, if obj is null) instance data for each * serializable class of object in stream, from superclass to subclass. * Expects that passHandle is set to obj's handle before this method is * called. */ private void readSerialData(Object obj, ObjectStreamClass desc) throws IOException { ObjectStreamClass.ClassDataSlot[] slots = desc.getClassDataLayout(); for (int i = 0; i < slots.length; i++) { ObjectStreamClass slotDesc = slots[i].desc;
if (slots[i].hasData) { if ((obj != null) && slotDesc.hasReadObjectMethod()) { Object oldObj = curObj; ObjectStreamClass oldDesc = curDesc; GetFieldImpl oldGet = curGet; curObj = obj; curDesc = slotDesc; curGet = null; bin.setBlockDataMode(true); try { slotDesc.invokeReadObject(obj, this); } catch (ClassNotFoundException ex) { /* * In most cases, the handle table has already * propagated a CNFException to passHandle at this * point; this call is included to address cases * where the custom readObject method has cons'ed and * thrown a new CNFException of its own. */ handles.Exception(passHandle, ex); } curObj = oldObj; curDesc = oldDesc; curGet = oldGet;
/* * defaultDataEnd may have been set indirectly by custom * readObject() method when calling defaultReadObject() or * readFields(); clear it to restore normal read behavior. */ defaultDataEnd = false; } else { defaultReadFields(obj, slotDesc); } if (slotDesc.hasWriteObjectData()) { skipCustomData(); } else { bin.setBlockDataMode(false); } } else { if (obj != null && slotDesc.hasReadObjectNoDataMethod()) { slotDesc.invokeReadObjectNoData(obj); } } } }如果你真的有兴趣自己去看源代码。
* Reads (or attempts to skip, if obj is null) instance data for each
* serializable class of object in stream, from superclass to subclass.
* Expects that passHandle is set to obj's handle before this method is
* called.
*/
private void readSerialData(Object obj, ObjectStreamClass desc)
throws IOException
{
ObjectStreamClass.ClassDataSlot[] slots = desc.getClassDataLayout();
for (int i = 0; i < slots.length; i++) {
ObjectStreamClass slotDesc = slots[i].desc;
if (slots[i].hasData) {
if ((obj != null) && slotDesc.hasReadObjectMethod()) {
Object oldObj = curObj;
ObjectStreamClass oldDesc = curDesc;
GetFieldImpl oldGet = curGet;
curObj = obj;
curDesc = slotDesc;
curGet = null; bin.setBlockDataMode(true);
try {
slotDesc.invokeReadObject(obj, this);
} catch (ClassNotFoundException ex) {
/*
* In most cases, the handle table has already
* propagated a CNFException to passHandle at this
* point; this call is included to address cases
* where the custom readObject method has cons'ed and
* thrown a new CNFException of its own.
*/
handles.Exception(passHandle, ex);
} curObj = oldObj;
curDesc = oldDesc;
curGet = oldGet;
/*
* defaultDataEnd may have been set indirectly by custom
* readObject() method when calling defaultReadObject() or
* readFields(); clear it to restore normal read behavior.
*/
defaultDataEnd = false;
} else {
defaultReadFields(obj, slotDesc);
}
if (slotDesc.hasWriteObjectData()) {
skipCustomData();
} else {
bin.setBlockDataMode(false);
}
} else {
if (obj != null && slotDesc.hasReadObjectNoDataMethod())
{
slotDesc.invokeReadObjectNoData(obj);
}
}
}
}如果你真的有兴趣自己去看源代码。
还有Serializable接口的问题,我的理解它只是个标志接口,没有任何东西,它是方便了二进制流和对象的转换
我尝试了将private改成public 结果不同,两个方法的println没有执行,我想知道为什么?
还有,能不能简单解释Serialization and Deserialization 如何实现“冷藏” 和“解冻”(Java 与模式中提到)如JavaBean就是如此的。
期待解释