布尔 getter 方法的有效 JavaBeans 名称
我知道大多数变量名都可以与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()
?
推荐答案
根据JavaBeans 规范 8.3.2 节:
According to the JavaBeans specification section 8.3.2:
布尔属性
此外,对于布尔属性,我们允许一个 getter匹配模式的方法:
public boolean is
这"isPropertyName
" 方法可能是提供而不是"get<PropertyName>
" 方法,也可以除了提供一个get
"方法.在任一情况下,如果 is
方法然后存在布尔属性我们将使用is<PropertyName>
"方法来读取属性值.一个示例布尔属性可能是:
公共布尔 isMarsupial();public void setMarsupial(boolean m);
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);
换句话说,除非从那以后发生了一些变化,否则 has
恐怕不是一个有效的前缀:(
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.
相关文章