[英]Extending a class that implements Serializable
If I extend a class that implements Serializable, do I need that class to also implement Serializable? 如果我扩展了一个实现Serializable的类,是否需要该类来实现Serializable?
For instance if I have, 例如,如果我有,
public class classToBeExtended implements Serializable
Then will this suffice? 那就足够了吗?
public class classThatWillExtend extends classToExtended
Or do I need to do this? 还是我需要这样做?
public class classThatWillExtend extends classToExtended implements Serializable
If any of a class's superclasses implements a given interface, then the subclass also implements that interface. 如果某个类的超类中的任何一个实现了给定的接口,则子类也将实现该接口。
Serializable
is not special in that regard, so no, the subclasses of a Serializable
class do not need to explicitly declare that they implement Serializable
. Serializable
在这方面不是特殊的,因此,不需要, Serializable
类的子类不需要显式声明它们实现了Serializable
。 They can so declare, but doing that makes no difference. 他们可以这样声明,但是这样做没有区别。
The other implication is that if you extend a Serializable
class, you should ensure that the subclass is indeed serializable itself. 另一个含义是,如果扩展
Serializable
类,则应确保子类本身确实可序列化。 For example, don't add non- transient
fields of non-serializable types unless you're prepared also to add the necessary methods to support them. 例如,不要添加不可序列化类型的非
transient
字段,除非您还准备添加必要的方法来支持它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.