布尔 getter 方法的有效 JavaBeans 名称 [英] Valid JavaBeans names for boolean getter methods

查看:28
本文介绍了布尔 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 the is<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.

这篇关于布尔 getter 方法的有效 JavaBeans 名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆