-1

我正在研究标记接口的行为,我使用以下链接制作了自己的标记接口,

关联

然后我比较 Serializable 接口的功能。

现在我有不扩展可序列化接口的 Employee 类

public class Employee {

public String name;
public String address;
public int number;
 }

然后我有另一个类 SerializeDemo 试图序列化对象

public class SerializeDemo {

    public static void main(String [] args)
       {
          Employee e = new Employee();
          e.name = "AAAA";
          e.address = "BBB, India";
          e.number = 101;

          try
          {
             FileOutputStream fileOut =  new FileOutputStream("employee.ser");
             ObjectOutputStream out = new ObjectOutputStream(fileOut);
             out.writeObject(e); //Error on this line( ERRORLINE)
             out.close();
             fileOut.close();
             System.out.printf("Serialized data is saved in /tmp/employee.ser");
          }catch(IOException i)
          {
              i.printStackTrace();
          }
       }
}



Error : java.io.NotSerializableException: com.serializable.Employee
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)......

现在,当我删除 ERRORLINE 行时,它会编译并创建一个新文件,但没有对象。我只是想知道如何 out.writeObject(e); 导致错误,ObjectOutputStream 是实现 Serializable 接口还是扩展到其他实现 Serializable 接口的类。

如何out.writeObject(e); 内部检查它是否可序列化?

任何帮助将不胜感激!

4

1 回答 1

1

您要序列化的对象必须标记为可序列化。

于 2016-02-28T11:56:00.183 回答