为什么编译器不能在编译时检测到关于Serializable声明的这个错误? [英] Why can't the compiler detect this error regarding Serializable declaration at compile time?

查看:223
本文介绍了为什么编译器不能在编译时检测到关于Serializable声明的这个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可序列化的JavaDoc interface:

From second paragraph of JavaDoc of Serializable interface :


为了允许序列化非序列化类的子类型,
子类型可以承担保存和恢复超类型的public,protected和(如果可访问)包
字段的状态
。只有当扩展的类
具有可访问的无参数构造函数来初始化
类的状态时,子类型才可能承担这种责任。如果这个
不是这样,声明一个类Serializable是一个错误。

To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.

为什么在编译时检测不到此错误?

Why can't this error be detected at compile time ?

推荐答案

编译器旨在根据语言的要求检查代码,而不是单个库。该语言可以更改为添加执行额外检查的关键字或注释,并且库可以使用理论上的那些。

The compiler is designed to check the code based on the requirements of the language, not an individual library. The language could be changed to add a keyword or annotation which perform additional checks and the library could use those in theory.

有用的是,如果编译器可以运行库特定的检查代码以它可以加载注释的方式(这是目前纯粹的数据)这种方式额外的检查可以在编译时执行,而不知道任何单独的库的行为。

What would be useful is if the compiler could run library specific checking code in the way it can load annotations (which are currently purely data) This way additional checks could be performed at compile time without it knowing the behaviour of any individual library.

这篇关于为什么编译器不能在编译时检测到关于Serializable声明的这个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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