Java 标记接口和注释的替换
当我实现自定义标记接口时,我想到了这一点。由于Serializable Marker接口用于java中的序列化,因此我创建了自己的Marker接口来在类级别设置标志Java 标记接口和注释的替换,java,Java,当我实现自定义标记接口时,我想到了这一点。由于Serializable Marker接口用于java中的序列化,因此我创建了自己的Marker接口来在类级别设置标志 public class MIOne implements Serializable,MarkerInterface{ private int one; private String str; public MIOne() { super(); this.one = 1;
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运算符)。我需要你的帮助,用其他更好更简单的解决方案来解决同样的问题。有可能吗?您为什么需要其他解决方案?表示元数据有两种方法:标记和注释。没有第三条路。看看这个问题,比如在父类中,我们是否有一个字段,比如说可以访问的标记,通过它我们可以得到我们有哪种标记。像这样,如果有任何其他解决方案,它消除了制造空白接口和其他逻辑实现的开销