Javabean约定 - 属性gId的方法命名 [英] Javabean convention - method naming for property gId
问题描述
如果我的Java类中有一个属性'gId',那么访问器方法应该命名为什么?
If I have a property 'gId' in my Java class what should the accessor method be named as?
getGId
是我的假设。
如果有一个属性 gURL
我认为它将是 getGURL
,哪种看起来很难看(虽然不是指女孩的另类拼写)。
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).
如果该属性只是 url
方法名称 getUrl
是好看的,是的,我不会将该属性命名为URL,这将使访问者再次丑陋 - getURL
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
我记得从Javabean Specification PDF中读取有关属性大写的内容以及涉及缩略语的案例,但不能再在PDF中找到它。
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?
推荐答案
您感兴趣的参考文献可以在 Beans规范中找到在第8.8节。
The reference you are interested in can be found in the Beans specification at Section 8.8.
话虽如此,它没有明确涵盖你的 gId
/的特定情况 GURL
。规范说要提供一个getter / setter,我们只需将首字母大写。要从getter / setter恢复属性,如果前两个字母是大写,则整个属性保持原样。否则我们将第一个字母的资本化。因此,您的getter将变为 getGURL
,但您的属性将被错误地从getter中恢复为 GURL
。你有同样的问题 gId
。
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.
我的建议是采用完全小写的属性,或者将小写前缀扩展为两个字母(例如, glURL
)。
My suggestion is to either adopt a completely lowercase property, or to extend the lowercase prefix to two letters (glURL
, for example).
这篇关于Javabean约定 - 属性gId的方法命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!