62,614
社区成员
发帖
与我相关
我的任务
分享
主要是做判断使用
/**
* Underlying writeObject/writeUnshared implementation.
*/
private void writeObject0(Object obj, boolean unshared)
throws IOException
{
//以下源码摘抄自1142行起
// remaining cases
if (obj instanceof String) {
writeString((String) obj, unshared);
} else if (cl.isArray()) {
writeArray(obj, desc, unshared);
} else if (obj instanceof Enum) {
writeEnum((Enum) obj, desc, unshared);
} else if (obj instanceof Serializable) { //这里对需要进行序列化的对象instanceOf判断,也就是判断该对象有没有实现标识接口Serializable
writeOrdinaryObject(obj, desc, unshared);
} else {
if (extendedDebugInfo) {
throw new NotSerializableException(
cl.getName() + "\n" + debugInfoStack.toString());
} else {
throw new NotSerializableException(cl.getName());
}
}
} finally {
depth--;
bout.setBlockDataMode(oldMode);
}
}
package cn.please.serializable;
import java.io.Serializable;
//实现此标识接口的类,其中不含任何方法
class A implements Serializable {
}
// 类B没有实现Serializable接口
class B {
}
public class Test {
public static void main(String[] args) {
A a = new A();
B b = new B();
// A的实例对象 a 同样可以作为是Serializable类型 ,类B则不行
System.out.println(a instanceof Serializable); //output: true
System.out.println(b instanceof Serializable); //output: false
}
}