Java 中 getter/setter 的命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2948083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Naming convention for getters/setters in Java
提问by Simon
if I have the following private member:
如果我有以下私人成员:
private int xIndex;
How should I name my getter/setter:
我应该如何命名我的 getter/setter:
getXindex()
setXindex(int value)
or
或者
getxIndex()
setxIndex(int value)
EDIT: or
编辑:或
getXIndex()
setXIndex(int value);
?
?
回答by Thomas Einwaller
The correct answer is
正确答案是
getxIndex()
setxIndex(int value)
if you want them to be used as properties according to section 8.8: Capitalization of inferred namesof the JavaBeans API specification(e.g. access them via ${object.xIndex} in a JSP.
如果您希望它们根据第8.8节:JavaBeans API 规范的推断名称的大写(例如,通过 JSP 中的 ${object.xIndex} 访问它们)用作属性。
回答by Love
In accordance with JavaBeans API specification from 1997it should be as Thomas Einwaller describes.
根据1997 年的 JavaBeans API 规范,它应该像 Thomas Einwaller 描述的那样。
private int xIndex;
public int getxIndex() { return xIndex; }
public void setxIndex(int xIndex) { this.xIndex = xIndex; }
This is unfortunate, getxand setxare not words, making for the rare case were code generated by IntelliJ also
gets a warning by IntelliJ. So while this conforms to the JavaBeans specification, it violates the convention for
naming a method.
In the rare case when this would form a word or acronym it would be disinformative, eg the methodsetiMessagemost
likely has nothing to do with SETI.
Using the only valid measurement of code quality(WTFs per minute),
I assess that this is bad code.
不幸的是,getx和setx不是单词,因此在极少数情况下,IntelliJ 生成的代码也会收到 IntelliJ 的警告。因此,虽然这符合 JavaBeans 规范,但它违反了命名方法的约定
。在极少数情况下,当这会形成一个单词或首字母缩略词时,它会失去信息,例如,该方法setiMessage很可能与SETI无关。使用唯一有效的代码质量度量(每分钟 WTF),我评估这是错误代码。
It all comes down to this sentence of the JavaBeans specification:
这一切都归结为 JavaBeans 规范的这句话:
However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone.
然而,为了支持偶尔使用所有大写的名字,我们检查名字的前两个字符是否都是大写的,如果是的话就不要管它。
Exactly what kind of use of all upper-case names this refers to is unclear to me. Field names should, according to convention, be camel cased. It seems to me that we generate unconventional method names in order to support unconventional field names as decided by a 20+ year old document.
我不清楚这所指的所有大写名称的确切用法。根据约定,字段名称应该 是驼峰式的。在我看来,我们生成非常规方法名称是为了支持由 20 多年历史的文档决定的非常规字段名称。
It should also be noted that even though it seems to be an overwhelming support for the JavaBeans specification in tools,
it is not exclusively used. Eg. Kotlin will not recognize xIndexas a property in the above example. Reversely, the
Kotlin property var xIndex = 0will result in the Java methods getXIndexand setXIndex. This seems to be a bug
according to the JetBrains support, but I fail to see how they can fix that without making a breaking change.
还应该注意的是,尽管它在工具中似乎是对 JavaBeans 规范的压倒性支持,但它并不是专门使用的。例如。Kotlin 不会xIndex在上面的示例中识别为属性。相反,Kotlin 属性var xIndex = 0将导致 Java 方法getXIndex和setXIndex. 根据 JetBrains 支持,这似乎是一个错误,但我看不出他们如何在不进行重大更改的情况下解决该问题。
Some tools that does support the JavaBeans specification has not always done so, eg Hymansonand Swagger Code Generatorhave been patched to conform to it. Even though IntelliJ generate accessors according to the JavaBeans specification, the examplein the documentation differs from it. Probably because people don't know about the standard and naturally prefers the normal method naming convention.
一些支持 JavaBeans 规范的工具并不总是如此,例如Hymanson和Swagger Code Generator已经被修补以符合它。尽管 IntelliJ 根据 JavaBeans 规范生成访问器 ,但文档中的示例与其不同。可能是因为人们不了解标准,自然更喜欢正常的方法命名约定。
So when should we follow the JavaBeans specification?When property names should be inferred by accessors by tools that
rely on this standard, then we might want to use it. For instance, Hymansonwill rely
on the property xIndexbeing accessed through getxIndexand setxIndexmethods unless we use annotations.
那么我们什么时候应该遵循 JavaBeans 规范呢?当访问器应该通过依赖此标准的工具推断属性名称时,我们可能想要使用它。例如,除非我们使用注释,否则Hymanson将依赖于xIndex被访问的属性getxIndex和setxIndex方法。
When should we avoid this standard?Per my recommendation: When the code should be read and understood by humans. Because to not use proper camel casing when naming methods is disinformative.
我们什么时候应该避免这个标准?根据我的建议:什么时候代码应该被人类阅读和理解。因为在命名方法时不使用正确的驼峰式大小写是误导性的。
If I would have it my way, we would use normal naming conventions, ie getXIndexand setXIndex. But, given the state
of things, the best solution I see is suggested by @vaxquis:
如果我将有它自己的路,我们会用正常的命名约定,即getXIndex和setXIndex。但是,鉴于目前的情况,@vaxquis 建议了我看到的最佳解决方案:
Name your field "indexX" or whatever else, your problem is solved... don't overcomplicate things - even if setxIndex is the correct way for Beans, having method named setxIndex increases the WTF factor of the code without giving you anything in return.
将您的字段命名为“indexX”或其他任何名称,您的问题就解决了......不要使事情过于复杂 - 即使 setxIndex 是 Beans 的正确方法,使用名为 setxIndex 的方法会增加代码的 WTF 因子而不会给您任何回报.
Any comments regarding the JavaBeans specification should, according the specification itself, be sent to [email protected].
根据规范本身,任何关于 JavaBeans 规范的评论都应该发送到 [email protected]。
回答by Samuel Yung
Should be:
应该:
getXIndex()
setXIndex(final int xIndex)
回答by ayush
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
方法应该是动词,大小写混合,首字母小写,每个内部单词的首字母大写。
回答by Mahmoud Turki
the correct answer is :
正确答案是:
getXIndex();
setXIndex(int value);
a screenshot from JAVA SE8 Oracle Certification Associate : enter image description here
来自 JAVA SE8 Oracle Certification Associate 的截图: 在此处输入图片描述
回答by Hervé Darritchon
You should use Introspector.decapitalize from package java.beans and you have no problem beacause it is compliant with java rules.
您应该使用包 java.beans 中的 Introspector.decapitalize 并且您没有问题,因为它符合 java 规则。
回答by Bipin Thakur
Eclipse ide automatically generates setters and getters as:
Eclipse ide 自动生成 setter 和 getter:
getxIndex()
setxIndex(int value)
Which is according to the java beans API specification.
这是根据 java bean API 规范。
回答by m_pGladiator
I think getXindex()is the best way. The getter should start with 'get', followed by the member name, with its first letter capitalized. Also the latest conventions I heard of, say that we should avoid multiple capital letters one after another. For example getHTMLtooltipis wrong. it should be getHtmlTooltipinstead. Also you should try to make all your members finaland there should not be a need of setters, since the class will be immutable ;)
我认为getXindex()是最好的方法。getter 应以“get”开头,后跟成员名称,首字母大写。还有我听说的最新约定,说我们应该一个接一个地避免多个大写字母。例如getHTMLtooltip是错误的。应该是getHtmlTooltip。此外,您应该尝试创建所有成员,final并且不需要 setter,因为该类将是不可变的 ;)

