当我实现自定义标记界面时,我想到了这件事。由于Serializable Marker接口用于java中的序列化,我创建了自己的Marker接口来在类级别设置标志。
public class MIOne implements Serializable,MarkerInterface{
private int one;
private String str;
public MIOne() {
super();
this.one = 1;
this.str = "MIOne";
}
Object writeObject() throws IOException {
if (!(this instanceof MarkerInterface)) {
FileOutputStream out = new FileOutputStream("D:\\testTransients.ser");
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(this);
} else {
System.out.println("Unable to Support Searialization");
}
return null;
}
Object readObject() throws IOException, ClassNotFoundException {
if (!(this instanceof MarkerInterface)) {
FileInputStream fis = new FileInputStream("D:\\testTransients.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
MIOne c = (MIOne) ois.readObject();
} else {
System.out.println("Unable to Support Searialization");
}
return null;
}
}
但是在这里我必须做一个空白的接口,而不是相应地实现我的逻辑(使用instanceOf运算符)。我需要你的帮助,用其他更好更简单的解决方案来解决同样的问题。这可能吗?