java中的类名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16537620/
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
Class name convention in java
提问by ankit
What is the naming convention of classes in java, for example should all classes be in upper case like MYCLASS.java ?
java中类的命名约定是什么,例如,所有类都应该像 MYCLASS.java 一样大写吗?
as some classes like com.sun.org.apache.bcel.internal.generic. ANEWARRAY. can be found in Sun's library as well
像com.sun.org.apache.bcel.internal.generic这样的一些类 。ANEWARRAY。也可以在 Sun 的图书馆中找到
Note: I have read all naming convention from Oraclebut I could not find anything which says we should name a class with All Uppercase.
注意:我已经阅读了Oracle 的所有命名约定,但我找不到任何说明我们应该使用全大写命名类的内容。
采纳答案by ankit
After browsing through many links I came to know there is no java convention which suggests us to name a class in all uppercases. However we do so there will not be any error.
在浏览了许多链接后,我才知道没有 Java 约定建议我们以所有大写字母命名一个类。但是我们这样做不会有任何错误。
One appropriate reason is mentioned by CloudyMarblethat we can use this convention if the abbreviation is more famous like HTML.
CloudyMarble提到了一个适当的原因,如果缩写像 HTML 那样更出名,我们可以使用这个约定。
Also I would like to add some info about ANEWARRAY, this class comes under Apache license Sourceand Create new array of references.
另外我想添加一些关于 ANEWARRAY 的信息,这个类来自 Apache 许可证源并创建新的引用数组。
回答by CloudyMarble
Class Names should be in CamelCase. Try to use nouns because a class is normally representing something in the real world.
类名应该是驼峰式的。尝试使用名词,因为一个类通常代表现实世界中的某些东西。
The Documentationstates the following:
该文件规定如下:
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
类名应该是名词,大小写混合,每个内部单词的第一个字母大写。尽量保持你的类名简单和描述性。使用完整的单词——避免首字母缩略词和缩写词(除非缩写词的使用比长格式更广泛,例如 URL 或 HTML)。
回答by Ozzie
Java has a very well described naming / coding convention.
Java 有一个很好描述的命名/编码约定。
You can look it up here http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html
你可以在这里查看http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html
Technically it doesn't matter how you name you classes as long as public classes are in a .java-source file with the same name as the class.
从技术上讲,只要公共类位于与类同名的 .java-source 文件中,您如何命名类并不重要。
回答by Richard Tingle
The quoted class com.sun.org.apache.bcel.internal.generic.ANEWARRAY looks to be from the deep innerworking of Java (internal.generic), i.e. not for developer use. As such its really outside of the naming convention. I can only speculate as to why its all in capitals, perhaps to emphasise this point that it shouldn't be used.
引用的类 com.sun.org.apache.bcel.internal.generic.ANEWARRAY 看起来来自 Java (internal.generic) 的深层内部工作,即不供开发人员使用。因此,它确实超出了命名约定。我只能推测为什么它都是大写的,也许是为了强调不应该使用它的这一点。
回答by Sanjaya Liyanage
Usually the best practise is to use Upper CamelCase.anyway there will be no compilation issues with other conventions.see more details about conventions
通常最佳实践是使用 Upper CamelCase。无论如何,使用其他约定不会有编译问题。请参阅有关约定的更多详细信息