Java 包名称中的单词分隔符的约定是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3179216/
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
What is the convention for word separator in Java package names?
提问by Jigar
How should one separate words in package names? Which of the following are correct?
包名中的一个单词应该如何分隔?以下哪些是正确的?
com.stackoverflow.my_package
(underscore)com.stackoverflow.my-package
(hyphens)com.stackoverflow.MyPackage
(CamelCase)
com.stackoverflow.my_package
(下划线)com.stackoverflow.my-package
(连字符)com.stackoverflow.MyPackage
(骆驼香烟盒)
What is the general standard?
一般标准是什么?
采纳答案by polygenelubricants
Here's what the official naming conventions document prescribes:
以下是官方命名约定文档的规定:
Packages
The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently
com
,edu
,gov
,mil
,net
,org
, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.
Examples
com.sun.eng
com.apple.quicktime.v2
edu.cmu.cs.bovik.cheese
套餐
唯一包名称的前缀总是用全小写的 ASCII 字母书写,并且应该是顶级域名之一,目前
com
,edu
,gov
,mil
,net
,org
, 或 ISO 中指定的标识国家/地区的英文双字母代码之一标准 3166,1981。包名称的后续组成部分根据组织自己的内部命名约定而有所不同。此类约定可能指定某些目录名称组件是部门、部门、项目、机器或登录名。
例子
com.sun.eng
com.apple.quicktime.v2
edu.cmu.cs.bovik.cheese
References
参考
Note that in particular, anything following the top-level domain prefix isn't specified by the above document. The JLS also agrees with this by giving the following examples:
请特别注意,上面的文档没有指定顶级域前缀之后的任何内容。JLS 也同意这一点,举出以下例子:
com.sun.sunsoft.DOE
gov.whitehouse.socks.mousefinder
com.JavaSoft.jag.Oak
org.npr.pledge.driver
uk.ac.city.rugby.game
com.sun.sunsoft.DOE
gov.whitehouse.socks.mousefinder
com.JavaSoft.jag.Oak
org.npr.pledge.driver
uk.ac.city.rugby.game
The following excerpt is also relevant:
以下摘录也是相关的:
In some cases, the internet domain name may not be a valid package name. Here are some suggested conventions for dealing with these situations:
- If the domain name contains a hyphen, or any other special character not allowed in an identifier, convert it into an underscore.
- If any of the resulting package name components are keywords then append underscore to them.
- If any of the resulting package name components start with a digit, or any other character that is not allowed as an initial character of an identifier, have an underscore prefixed to the component.
在某些情况下,互联网域名可能不是有效的包名称。以下是处理这些情况的一些建议约定:
- 如果域名包含连字符或标识符中不允许的任何其他特殊字符,请将其转换为下划线。
- 如果任何结果包名称组件是关键字,则在其后添加下划线。
- 如果任何结果包名称组件以数字或任何其他不允许作为标识符首字符的字符开头,请在组件前加上下划线。
References
参考
回答by bragboy
All three are not the conventions.
这三个都不是约定俗成的。
Use com.stackoverflow.mypackage
.
使用com.stackoverflow.mypackage
.
The package names do not follow camel casing or underscores or hyphens package naming convention.
包名称不遵循驼峰式大小写或下划线或连字符包命名约定。
Also, Google Java Style Guidespecifies exactly the same (i.e. com.stackoverflow.mypackage
) convention:
此外,Google Java Style Guide指定了完全相同的(即com.stackoverflow.mypackage
)约定:
5.2.1 Package names
Package names are all lowercase, with consecutive words simply concatenated together (no underscores). For example,
com.example.deepspace
, notcom.example.deepSpace
orcom.example.deep_space
.— Google Java Style Guide: 5.2 Rules by identifier type: 5.2.1 Package names.
5.2.1 包名
包名都是小写的,连续的单词简单地连接在一起(没有下划线)。例如,
com.example.deepspace
、不com.example.deepSpace
或com.example.deep_space
。— Google Java Style Guide: 5.2 Rules by identifier type: 5.2.1 Package names。
回答by Andreas Dolk
The official naming conventions aren't that strict, they don't even 'forbid' camel case notation except for prefix (com
in your example).
官方命名约定并不那么严格,除了前缀(com
在您的示例中)之外,它们甚至不“禁止”驼峰式大小写符号。
But I personally would avoid upper case letters and hyphenations, even numbers. I'd choose com.stackoverflow.mypackage
like Bragboy suggested too.
但我个人会避免使用大写字母和连字符,甚至是数字。我也会com.stackoverflow.mypackage
像 Bragboy 建议的那样选择。
(hyphenations '-' are not legal in package names)
(连字符“-”在包名中不合法)
EDIT
编辑
Interesting - the language specification has something to say about naming conventions too.
有趣 - 语言规范也有关于命名约定的内容。
In Chapter 7.7 Unique Package Nameswe see examples with package names that consist of upper case letters (so CamelCase notation would be OK) and they suggest to replace hyphonation by an underscore ("mary-lou" -> "mary_lou") and prefix java keywords with an underscore ("com.example.enum" -> "com.example._enum")
在第 7.7 章唯一包名称中,我们看到包名称由大写字母组成的示例(因此 CamelCase 表示法可以),他们建议用下划线(“mary-lou”->“mary_lou”)和前缀 java 替换连字符带下划线的关键字(“com.example.enum”->“com.example._enum”)
Some more examples for upper case letters in package names can be found in chapter 6.8.1 Package Names.
更多关于包名中大写字母的例子可以在第6.8.1章包名中找到。
回答by Himanshu Mori
Anyone can use underscore _(its Okay)
任何人都可以使用下划线_(没关系)
No one should use hypen -(its Bad practice)
没有人应该使用连字符-(这是不好的做法)
No one should use capital letters inside package names (Bad practice)
任何人都不应该在包名中使用大写字母(不好的做法)
NOTE: Here "Bad Practice" is meant for technically you are allowed to use that, but conventionally its not in good manners to write.
注意:这里“不良做法”的意思是从技术上讲,您可以使用它,但通常不以良好的方式编写。
Source: Naming a Package(docs.oracle)
回答by jpangamarca
Underscores look ugly in package names. For what is worth, in case of names compound of three or more words I use initials (for example: com.company.app.ingresoegresofijo (ingreso/egreso fijo) -> com.company.app.iefijo
) and then document the package purpose in package-info.java
.
包名中的下划线看起来很难看。对于什么是值得的,如果名称由三个或更多单词组成,我使用首字母(例如:)com.company.app.ingresoegresofijo (ingreso/egreso fijo) -> com.company.app.iefijo
,然后在package-info.java
.
回答by CraUmm
Concatenation of words in the package name is something most developers don't do.
包名中的单词串联是大多数开发人员不做的事情。
You can use something like.
你可以使用类似的东西。
com.stackoverflow.mypackage
com.stackoverflow.mypackage
Refer JLS Name Declaration
参考JLS 名称声明