boolean(布尔) - getter是vs get

Hub*_*ert 8 java wsimport

看起来每个人都说正确的吸气剂:

  • 原始布尔 - > getter
  • object Boolean - > getter get

例:

public class Test {

    private boolean primitive;
    private Boolean object;

    public boolean isPrimitive() {
         return primitive;
    }
    public Boolean getObject() {
        return object;
    }
    //..
}
Run Code Online (Sandbox Code Playgroud)

题:

是否有任何规范或文档声明这是正确的,这是为布尔值指定getter的方法?或者这只是一个常见的假设?

我问becouse例如的wsimport生成的getter 布尔对象.这是一个工具错误,还是这是允许的和正确的?

另一方面,一些framweork与这样的吸气剂不能正常工作.例如JSF(EL)或Dozer.

Vin*_*tin 8

字段的getter方法boolean myFieldgetMyfield()isMyField()(由用户自行选择).我个人使用第二种格式,就像许多源代码生成工具一样.

此格式是标准,它在JavaBeans规范中定义.请参阅本文档的第8.3.2节:http: //download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/

从文档引用:

In addition, for boolean properties, we allow a getter method to match the pattern:

  public boolean is<PropertyName>();
Run Code Online (Sandbox Code Playgroud)

文档没有讨论像Boolean类这样的原始包装器.


The*_*ind 5

public boolean isPrimitive() {
     return primitive;        // "is" used because the value can be either true or false. Its like asking isTrue??
}
public Boolean getObject() {
    return object;           // "get" is used because the value returned can be either true, false or null. So, the third state 'null' makes you wonder if 'is' should be used or 'get'. get is more appropriate as Boolean can also have null.
Run Code Online (Sandbox Code Playgroud)

但坦率地说,它留给了开发人员。在布尔值上使用getBoolean没有任何“错误”(“ is”更有意义。就是这样。)