我有以下类包含下面指定的字段。我的问题是,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/