Javabean convention - method naming for property gId

五迷三道 提交于 2019-11-28 05:34:05

问题


If I have a property 'gId' in my Java class what should the accessor method be named as?

getGId is what I assume.

If there were a property gURL I think it would be getGURL, which kind of looks ugly (not referring to the alternative spelling of girl though).

If the property was just url the method name getUrl is good on the eye and yeah I would not name the property as URL in the first place which would make the accessor ugly again - getURL

I remember reading from the Javabean Specification PDF somewhere about capitalization of properties and also cases involving acronyms but cant find it in the PDF anymore.

Does anyone have a reference to it or please confirm if I am right in what I am saying?


回答1:


The reference you are interested in can be found in the Beans specification at Section 8.8.

That being said, it does not explicitly cover your particular case of gId/gURL. The specification says that to provide a getter/setter, we simply capitalize the first letter. To recover the property from the getter/setter, if the first two letters are uppercase, then the whole property is left as-is. Otherwise we decapitalize the first letter. So your getter would become getGURL, but your property would be incorrectly recovered from the getter as GURL. You have the same problem with gId.

Therefore it seems that the specification does not allow you to provide a consistent translation of any property with a first lowercase character followed by an uppercase character.

My suggestion is to either adopt a completely lowercase property, or to extend the lowercase prefix to two letters (glURL, for example).




回答2:


To be more concret (and as said by Luca and here), spec tells that there is a method which can tell you: java.beans.Introspector.decapitalize(String).



来源:https://stackoverflow.com/questions/11114147/javabean-convention-method-naming-for-property-gid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!