布尔 getter 方法的有效 JavaBeans 名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/799280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Valid JavaBeans names for boolean getter methods
提问by
I know most variable names will work with "is", such as isBlue()
, but is "has" also a valid prefix, like hasProperty()
?
我知道大多数变量名称都可以与“is”一起使用,例如isBlue()
,但是“has”也是一个有效的前缀,例如hasProperty()
?
回答by Ryan Guill
This is somewhat subjective, but yes, I would say "has" is a perfectly valid prefix for a Boolean property.
这有点主观,但是是的,我会说“has”是布尔属性的完全有效的前缀。
editthe 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.
编辑问题,如所问,没有提到 javabeans 规范,所以我的回答没有解决问题的那个方面。因此上面的答案。
回答by Jon Skeet
According to the JavaBeans specificationsection 8.3.2:
根据JavaBeans 规范第 8.3.2 节:
Boolean properties
In addition, for boolean properties, we allow a getter method to match the pattern:
public boolean is<PropertyName>();
This "
isPropertyName
" method may be provided instead of a "get<PropertyName>
" method, or it may be provided in addition to a "get<PropertyName>
" method. In either case, if theis<PropertyName>
method is present for a boolean property then we will use the "is<PropertyName>
" method to read the property value. An example boolean property might be:public boolean isMarsupial(); public void setMarsupial(boolean m);
布尔属性
此外,对于布尔属性,我们允许使用 getter 方法来匹配模式:
public boolean is<PropertyName>();
isPropertyName
可以提供 该“ ”方法来代替“get<PropertyName>
”方法,或者除了“get<PropertyName>
”方法之外还可以提供该“ ”方法。在任何一种情况下,如果该is<PropertyName>
方法适用于布尔属性,那么我们将使用“is<PropertyName>
”方法来读取属性值。一个示例布尔属性可能是:public boolean isMarsupial(); public void setMarsupial(boolean m);
In other words, unless something has changed since then, has
isn't a valid prefix I'm afraid :(
换句话说,除非从那时起发生了变化,否则has
恐怕不是有效的前缀:(
It's possible that sometools and libraries will recognise such properties anyway, but it's not a good idea to rely on it.
这可能是一些工具和库无论如何都会认识到这种特性,但它不依赖于它是一个好主意。
回答by Bozho
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:
Jon Skeet 指出,根据规范,它是无效的。此外,canX
、shouldX
、 等也无效。这是相当不幸的。这是一种检查给定属性是否具有有效 getter 的方法:
BeanInfo info = Introspector.getBeanInfo(Item.class);
Item itm = new Item();
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
System.out.println(pd.getName() + " : " + pd.getReadMethod());
}
The class Item
should be a javabean with a foo property, and a getter. If the read method is null
, it means there is no valid getter defined according to the javabeans spec.
该类Item
应该是一个带有 foo 属性和一个 getter 的 javabean。如果 read 方法是null
,则表示没有根据 javabeans 规范定义的有效 getter。