在设计抽象类时应该考虑序列化问题吗? [英] Should I consider serialization issues when designing abstract classes?

查看:336
本文介绍了在设计抽象类时应该考虑序列化问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,这个问题来自于Eclipse建议在抽象类中添加一个串行版本的UID。



由于类是这个类的抽象实例将永远不存在,所以他们将不会被序列化(只有派生类才会)。



所以我的问题是放置一个 @SuppressWarnings(serial)一个抽象类,扩展程序将被序列化(并且它们将具有serialVersionUID)?



还有在设计抽象时要注意的任何其他问题



谢谢。

解决方案

每个可序列化类都需要一个 serialVersionUID 。如果一个对象是一个本身扩展可序列化类的运行时类型,则有两个 serialVersionUID 。所以,是的,你需要它,除非你决定不支持跨版本的序列化(如Swing)。


Generally this question comes from Eclipse suggesting to add a serial version UID on an abstract class.

Since the class is abstract instances of this class will never exist thus they will never be serialized (only derived classes will be).

So my question is it safe to place a @SuppressWarnings("serial") on an abstract class who's extenders will be serialized (and they will have serialVersionUIDs) ?

Also any other issues to be careful about when designing an abstract class who's extenders will be serialized are welcome.

Thanks.

解决方案

Every serialisable class needs a serialVersionUID. If an object is of a runtime type that itself extends a serialisable class, then there are two serialVersionUIDs involved. So, yes you need it, unless you decide not to support serialisation across versions (like Swing).

这篇关于在设计抽象类时应该考虑序列化问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆