布尔 getter 方法的有效 JavaBean 名称
我知道大多数变量名称都可以使用“is”,例如 isBlue()
,但是“has”也是有效的前缀吗,例如 hasProperty()
?
I know most variable names will work with "is", such as isBlue()
, but is "has" also a valid prefix, like hasProperty()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 JavaBeans 规范 部分8.3.2:
换句话说,除非从那时起发生了一些变化,否则
has
恐怕不是一个有效的前缀:(一些工具和库无论如何都会识别这些属性,但依赖它并不是一个好主意。
According to the JavaBeans specification section 8.3.2:
In other words, unless something has changed since then,
has
isn't a valid prefix I'm afraid :(It's possible that some tools and libraries will recognise such properties anyway, but it's not a good idea to rely on it.
Jon Skeet 指出,根据规范,它是无效的。 此外,
canX
、shouldX
等都是无效的。 这是相当不幸的。 以下是检查给定属性是否具有有效 getter 的方法:Item
类应该是具有 foo 属性和 getter 的 javabean。 如果 read 方法为null
,则表示没有根据 javabeans 规范定义有效的 getter。Jon Skeet noted that according to the specification it is not valid. Also,
canX
,shouldX
, and the likes are not valid. Which is rather unfortunate. Here is a way to check whether a given property has a valid getter:The class
Item
should be a javabean with a foo property, and a getter. If the read method isnull
, it means there is no valid getter defined according to the javabeans spec.这有点主观,但是,是的,我想说“has”是布尔属性的完全有效的前缀。
编辑问题,按照要求,没有提到javabeans规范,所以我的回答没有解决问题的这方面。 于是就有了上面的答案。
This is somewhat subjective, but yes, I would say "has" is a perfectly valid prefix for a Boolean property.
edit the question, as asked, did not mention the javabeans specification and so my answer did not address that aspect of the question. Hence the answer above.