0

当我实现自定义标记接口时,我想到了这件事。由于 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 运算符)。我需要你的帮助,用其他更好、更简单的解决方案来解决同样的问题。那可能吗?

4

0 回答 0