Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

java - Valid JavaBeans names for boolean getter methods

I know most variable names will work with "is", such as isBlue(), but is "has" also a valid prefix, like hasProperty()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the JavaBeans specification section 8.3.2:

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);

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...