Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我看到的每个关于对象序列化的示例都包括一个所有字段构造函数以及每个字段的 getter 和 setter 方法。这是必需品吗?您需要做什么才能使 java 对象可序列化,使用可序列化接口对其进行标记并确保所有非瞬态字段都是可序列化的。
除了您提到的基本步骤之外,不需要任何东西来使 Object 可序列化。对于某种形式的 getter/setter 或构造函数的存在没有要求(尽管可能有其他正当理由需要这些)。
构造函数/getter/setter 不是强制性的。非瞬态集合虽然可序列化包含对不可序列化对象的引用,但会导致失败。