I know most variable names will work with "is", such as isBlue()
, but is "has" also a valid prefix, like hasProperty()
?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
According to the JavaBeans specification section 8.3.2:
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.
This is somewhat subjective, but yes, I would say "has" is a perfectly valid prefix for a Boolean property.
edit the question, as asked, did not mention the javabeans specification and so my answer did not address that aspect of the question. Hence the answer above.
Jon Skeet noted that according to the specification it is not valid. Also,
canX
,shouldX
, and the likes are not valid. Which is rather unfortunate. Here is a way to check whether a given property has a valid getter:The class
Item
should be a javabean with a foo property, and a getter. If the read method isnull
, it means there is no valid getter defined according to the javabeans spec.