Java 包名称中单词分隔符的约定是什么?
包名中的单词应该如何分隔?以下哪项是正确的?
com.stackoverflow.my_package
(Snake Case
使用下划线)com.stackoverflow.my-package
(Kebab Case
使用连字符)com.stackoverflow.myPackage
(Camel Case
)com.stackoverflow.MyPackage
(Pascal Case
)
什么是通用标准?
How should one separate words in package names? Which of the following are correct?
com.stackoverflow.my_package
(Snake Case
using underscore)com.stackoverflow.my-package
(Kebab Case
using hyphens)com.stackoverflow.myPackage
(Camel Case
)com.stackoverflow.MyPackage
(Pascal Case
)
What is the general standard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这三个都不是惯例。
使用com.stackoverflow.mypackage。
包名称不遵循驼峰式大小写、下划线或连字符包命名约定。
此外,Google Java 样式指南 指定了完全相同的内容(即
com.stackoverflow.html)。 mypackage
) 约定:All three are not the conventions.
Use
com.stackoverflow.mypackage
.The package names do not follow camel casing or underscores or hyphens package naming convention.
Also, Google Java Style Guide specifies exactly the same (i.e.
com.stackoverflow.mypackage
) convention:以下是官方命名约定文档的规定:
参考文献
请特别注意,顶部后面的任何内容上述文档未指定 -level 域名前缀。 JLS 也同意这一点,并给出了以下例子:
以下摘录也相关:
参考文献
Here's what the official naming conventions document prescribes:
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:
The following excerpt is also relevant:
References
任何人都可以使用下划线 _ (没关系)
没有人应该使用连字符 - (这是一种不好的做法,可能会导致您按照评论部分中的 @glglgl 出现错误)
没有人应该在包名称中使用大写字母(不好的做法)
注意:这里的“不好的做法”是指从技术上讲,您可以使用它,但按照惯例,它的书写方式并不好。
来源:命名包(docs.oracle)
Anyone can use underscore _ (its Okay)
No one should use hypen - (its Bad practice and it may result you in error as per @glglgl in comments section)
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)
官方命名约定并不那么严格,他们甚至不“禁止”驼峰式大小写表示法,除了前缀(示例中的
com
)。但我个人会避免使用大写字母
和连字符,甚至数字。我也会像 Bragboy 建议的那样选择 com.stackoverflow.mypackage 。(连字符“-”在包名称中是不合法的)
编辑
有趣 - 语言规范也有关于命名约定的内容。
在 第 7.7 章唯一包名称中,我们看到包名称由大写字母组成的示例(因此驼峰命名法就可以了),他们建议用下划线替换连字符(“mary-lou”->“mary_lou”),并用下划线作为 java 关键字的前缀(“com.java”)。 example.enum" -> "com.example._enum")
包名称中大写字母的更多示例可以在 6.8.1 包名称。
The official naming conventions aren't that strict, they don't even 'forbid' camel case notation except for prefix (
com
in your example).But I personally would avoid upper case letters
and hyphenations, even numbers. I'd choosecom.stackoverflow.mypackage
like Bragboy suggested too.(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 Names we see examples with package names that consist of upper case letters (so CamelCase notation would be OK) and they suggest replacing hyphenation by an underscore ("mary-lou" -> "mary_lou") and prefix java keywords with an underscore ("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.
包名中的下划线看起来很难看。就其价值而言,如果名称由三个或更多单词组成,我会使用缩写(例如:
com.company.app.ingresoegresofijo (ingreso/egreso fijo) -> com.company.app.iefijo< /code>),然后在
package-info.java
中记录包的用途。Underscores look ugly in package names. For what it's 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 inpackage-info.java
.包名称中的单词串联是大多数开发人员不会做的事情。
你可以使用类似的东西。
com.stackoverflow.mypackage
请参阅JLS 名称声明
Concatenation of words in the package name is something most developers don't do.
You can use something like.
com.stackoverflow.mypackage
Refer JLS Name Declaration