FindBugs希望readObject(...)是私有的序列化,为什么? [英] FindBugs wants readObject(...) to be private for serialization, why?

查看:157
本文介绍了FindBugs希望readObject(...)是私有的序列化,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些代码上运行findbugs并且它说readObject(...)方法必须是私有的才能被调用以进行序列化/反序列化?为什么?如果它被公开会有什么问题?

I am running findbugs on some code and it says the readObject(...) method must be private to be invoked for serialization/unserialization? Why? What is the problem if it is made public?

推荐答案

关于 readObject()/ writeObject()私有,这是交易:如果你的班级延伸了一些类Foo; Foo还实现了 readObject()/ writeObject(),并且Bar还实现了 readObject()/ writeObject()

About readObject()/writeObject() being private, here's the deal: if your class Bar extends some class Foo; Foo also implements readObject()/writeObject() and Bar also implements readObject()/writeObject().

现在,当一个Bar对象被序列化或反序列化时,JVM需要为两者调用 readObject()/ writeObject()自动Foo和Bar(即无需显式调用这些超类方法)。但是,如果这些方法不是私有的,那么它就变成了方法覆盖,并且JVM不能再调用子类对象上的超类方法。

Now, when a Bar object is serialized or deserialized, JVM needs to call readObject()/writeObject() for both Foo and Bar automatically (i.e. without you needing to call these super class methods explicitly). However, if these methods are anything but private, it becomes method overriding, and JVM can no longer call the super class methods on the sub class object.

因此它们必须是私人!

这篇关于FindBugs希望readObject(...)是私有的序列化,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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