你能解释一下“isXxx"吗?Java中的方法名称? [英] Can you explain the "isXxx" method names in Java?
问题描述
在其中一个规范中是否有任何对以is"开头的方法的引用,而方法名称的后缀是属性的名称(类似于 Java bean 的 getter/setter 方法)?
Is there in one of the specifications any reference to methods that start with "is", while the suffix of the method's name is a property's name (similar to getter/setter methods of Java beans)?
例如:
public boolean isConditionTrue() {
...
...
}
private boolean conditionTrue;
谢谢!
推荐答案
仅对原始布尔值有效.以下是规范的摘录:
is only valid for primitive boolean. Here is an excerpt from the spec:
8.3.2 布尔属性此外,对于布尔属性,我们允许使用 getter 方法来匹配模式:公共布尔是();可以提供这个is"方法而不是get"方法od,或者它可以作为get"方法的补充.在任何一种情况下,如果布尔属性存在is"方法,那么我们将使用is"方法读取属性值.一个示例布尔属性可能是:public boolean isMarsupial();public void setMarsupial(boolean m);
8.3.2 Boolean properties In addition, for boolean properties, we allow a getter method to match the pattern: public boolean is(); This "is" method may be provided instead of a "get" meth- od, or it may be provided in addition to a "get" method. In either case, if the "is" method is present for a boolean property then we will use the "is" method to read the property value. An example boolean property might be: public boolean isMarsupial(); public void setMarsupial(boolean m);
注意使用 isXxx() : Boolean
函数,如果你打算将它们与 JSTL 标签之类的东西结合使用(使用 ${object.xxx}
语法)).他们不会拿它,你必须将它修改为 getXxx() : Boolean
.
Be aware of using isXxx() : Boolean
functions if you are going to use them in conjunction with things like JSTL tags (using ${object.xxx}
syntax). They won't pick it up and you have to modify it to getXxx() : Boolean
.
这篇关于你能解释一下“isXxx"吗?Java中的方法名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!