Java getter 的名称 returns 布尔值 class 根据命名约定
Java name for getter that returns Boolean class according to naming convention
我注意到对于 getter,return Boolean
(不是 boolean
!)netbeans 生成 getter 和 "get"字首。例如:
private Boolean main;
public Boolean getMain(){
return this.main;
}
这是错误的吗(根据命名约定)?或者 "is" 前缀仅用于原始类型?
Netbeans 在这里没有错 - 对于 Boolean
object 属性 get
是正确的。一方面,属性 值可能是 null
,在这种情况下 is
没有意义。
javabeans spec 允许 is
类型 boolean
作为一种特殊情况,并且 没有提到 Boolean
。假设这种特殊情况扩展到 Boolean
个对象是无效的。
这取决于包含该方法的 class 是否被视为 JavaBean。
如果您希望它是一个 JavaBean,那么 是准确的。
否则没有对错。应使用 get
还是 is
(或其他)取决于合同和方法的目的。 Eran 的第一条评论是:
There's no right or wrong here. I think isMain or hasMain or supportsMain (depnding on what main means) are more descriptive. I don't think it should make a different whether it's boolean or Boolean.
你问约定是什么,我会说约定是命名方法尽可能具有描述性和语义准确。
详细说明 is vs get:
如果该方法旨在作为大写字母 B Boolean
属性 的通用访问器,使值 null
更有意义使用 get
.
如果该方法旨在给出其他内部(如本例中 private
)非空标志的状态,我认为 is
将是适当的前缀。 (虽然我可能会使用 boolean
作为 return 值,除非有一个常见的用例,例如 isMain().hashCode()
或类似的东西。)
在 Boolean
对象上,您可以应用许多方法:toString
、equals
、valueOf
..
您的问题没有完整的答案,这实际上取决于用法和谁调用了该方法。有意义的是:
public boolean isMain(){
return this.main.booleanValue();
}
但如果您的逻辑不能确保 main
可以具有 null
值,那么 get
是一个很好的前缀。
正确。 Boolean 是原始数据类型 boolean 的包装器 class。所以布尔会return对象。同样 get
用于对象也像原始数据类型。
这里引用了真实的 JavaBeans specification document:
8.3.2 Boolean properties
In addition, for boolean properties, we allow a getter method to match the pattern:
public boolean is<PropertyName>();
This is<PropertyName>
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 the is<PropertyName>
method is present for a boolean property then we will use the is<PropertyName>
method to read the property value.
请注意,这适用于 boolean
而不是 Boolean
值。另请注意 is
是 允许的 替代 get
并且 get
总是合适的.
根据OCP Oracle Certified Professional Java SE 8 Programmer II Study Guide一书:
JavaBean 中可以正确包含以下哪项?
public boolean isPlaying() { return playing; }
public boolean getPlaying() { return playing; }
public Boolean isDancing() { return dancing; }
第一行是正确的,因为它为布尔变量定义了正确的 getter。
第二个例子也是正确的,因为 boolean 可以使用 is 或 get。 第三行是
不正确,因为布尔包装器应该以 get 开头,因为它是一个对象。
我注意到对于 getter,return Boolean
(不是 boolean
!)netbeans 生成 getter 和 "get"字首。例如:
private Boolean main;
public Boolean getMain(){
return this.main;
}
这是错误的吗(根据命名约定)?或者 "is" 前缀仅用于原始类型?
Netbeans 在这里没有错 - 对于 Boolean
object 属性 get
是正确的。一方面,属性 值可能是 null
,在这种情况下 is
没有意义。
javabeans spec 允许 is
类型 boolean
作为一种特殊情况,并且 没有提到 Boolean
。假设这种特殊情况扩展到 Boolean
个对象是无效的。
这取决于包含该方法的 class 是否被视为 JavaBean。
如果您希望它是一个 JavaBean,那么
否则没有对错。应使用 get
还是 is
(或其他)取决于合同和方法的目的。 Eran 的第一条评论是:
There's no right or wrong here. I think isMain or hasMain or supportsMain (depnding on what main means) are more descriptive. I don't think it should make a different whether it's boolean or Boolean.
你问约定是什么,我会说约定是命名方法尽可能具有描述性和语义准确。
详细说明 is vs get:
如果该方法旨在作为大写字母 B
Boolean
属性 的通用访问器,使值null
更有意义使用get
.如果该方法旨在给出其他内部(如本例中
private
)非空标志的状态,我认为is
将是适当的前缀。 (虽然我可能会使用boolean
作为 return 值,除非有一个常见的用例,例如isMain().hashCode()
或类似的东西。)
在 Boolean
对象上,您可以应用许多方法:toString
、equals
、valueOf
..
您的问题没有完整的答案,这实际上取决于用法和谁调用了该方法。有意义的是:
public boolean isMain(){
return this.main.booleanValue();
}
但如果您的逻辑不能确保 main
可以具有 null
值,那么 get
是一个很好的前缀。
正确。 Boolean 是原始数据类型 boolean 的包装器 class。所以布尔会return对象。同样 get
用于对象也像原始数据类型。
这里引用了真实的 JavaBeans specification document:
8.3.2 Boolean properties
In addition, for boolean properties, we allow a getter method to match the pattern:
public boolean is<PropertyName>();
This
is<PropertyName>
method may be provided instead of aget<PropertyName>
method, or it may be provided in addition to aget<PropertyName>
method. In either case, if theis<PropertyName>
method is present for a boolean property then we will use theis<PropertyName>
method to read the property value.
请注意,这适用于 boolean
而不是 Boolean
值。另请注意 is
是 允许的 替代 get
并且 get
总是合适的.
根据OCP Oracle Certified Professional Java SE 8 Programmer II Study Guide一书:
JavaBean 中可以正确包含以下哪项?
public boolean isPlaying() { return playing; }
public boolean getPlaying() { return playing; }
public Boolean isDancing() { return dancing; }
第一行是正确的,因为它为布尔变量定义了正确的 getter。 第二个例子也是正确的,因为 boolean 可以使用 is 或 get。 第三行是 不正确,因为布尔包装器应该以 get 开头,因为它是一个对象。