Java 序列化 : Must E be serializable in ArrayList<E>?

标签 java serializable

我有以下类包含下面指定的字段。我的问题是,Admin、Worker 和我所有其他自定义类是否必须实现 Serializable 才能使 MyClass 实现可序列化?

public class MyClass implements java.io.Serializable {
  private static final long serialVersionUID = 1L;
  ArrayList<Admin> admins;
  ArrayList<Worker> workers;
  ArrayList<Manager> managers;
  ArrayList<Secretary> secretaries;
  ArrayList<Category> categories;
  HashMap<Issue, HashMap<Category,Manager>> ManagedIssues;
  private static MyClass instance;
      ...
}

最佳答案

My question is, must Admin, Worker and all my other self-defined classes implement Serializable for MyClass to be Serializable?

是的。他们必须是。

ArrayList 默认已经实现了Serializable接口(interface)。但是您需要为您使用的类型实现 Serializable 接口(interface)。

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable.

关于Java 序列化 : Must E be serializable in ArrayList<E>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20765534/

相关文章:

java - 调用 Spring Data Rest Repository 方法不返回链接

java - 未找到 Web 服务器 AngularJs GET 404

java - Spring Cloud AWS cglib问题

java - 为什么我的对象没有正确反序列化?

python - simplejson.dumps(dict) 抛出 "not JSON serializable"

java - 使用 Java 小程序的屏幕截图

java - 如何查找静态函数被调用 Mockito 的次数

java - Mvel的编译表达式如何存储在数据库中

java - 在 Android 中持久化一个 Parcelable 对象

scala - 使用匿名函数时的 Spark TaskNotSerializable