简体   繁体   English

boolean (Boolean) - getter is vs get

[英]boolean (Boolean) - getter is vs get

It looks like everyone says that right getter for:看起来每个人都说正确的吸气剂适用于:

  • primitive boolean -> getter is原始布尔值 -> getter
  • object Boolean -> getter get object Boolean -> getter get

Example:例子:

public class Test {

    private boolean primitive;
    private Boolean object;

    public boolean isPrimitive() {
         return primitive;
    }
    public Boolean getObject() {
        return object;
    }
    //..
}

Question:题:

Is there any spec or document that states this is correct and this is the way to specify getters for boolean values?是否有任何规范或文档说明这是正确的,并且这是为布尔值指定 getter 的方法? Or this is only a common assumption?或者这只是一个普遍的假设?

I'm asking becouse for example wsimport generates getter is for Boolean object.我问是因为例如wsimport生成的 getter用于 Boolean 对象的。 Is this a tool bug, or this is allowed and correct?这是一个工具错误,还是允许且正确的?

In the other hand some framweorks don't work properly with such getters.另一方面,一些框架无法与此类吸气剂一起正常工作。 For example JSF (EL) or Dozer.例如 JSF (EL) 或 Dozer。

The getter method for the field boolean myField is getMyfield() or isMyField() (it's up to the user to choose).字段boolean myField的 getter 方法是getMyfield()isMyField() (由用户选择)。 I personally use the second format, as many source code generating tools do.我个人使用第二种格式,就像许多源代码生成工具一样。

This format is a standard, it is defined in the JavaBeans specification.这种格式是一种标准,它在JavaBeans规范中定义。 See the section 8.3.2 of this documentation: http://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/请参阅本文档的第 8.3.2 节http : //download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/

Quote from the docs:来自文档的引用:

 In addition, for boolean properties, we allow a getter method to match the pattern: public boolean is<PropertyName>();

The documentation doesn't talk about the primitive wrappers like the Boolean class.文档没有讨论像Boolean类这样的原始包装器。

// "is" used because the value can be either true or false. It's like asking isTrue?
public boolean isPrimitive() {
     return primitive;
}

// "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.
public Boolean getObject() {
    return object;
}

But frankly, it's left to the developer.但坦率地说,这留给了开发人员。 There's nothing "wrong" in using getBoolean() on a boolean value ( is makes more sense, that's it).没有什么“错误”使用getBoolean()一个布尔值( is更有意义,就是这样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM