在包名中如何区分单词?下列哪个选项是正确的?
com.stackoverflow。my_package(使用下划线的Snake Case) com.stackoverflow。my-package(连字符串) com.stackoverflow.myPackage(驼色案例) com.stackoverflow.MyPackage (Pascal案例)
一般标准是什么?
在包名中如何区分单词?下列哪个选项是正确的?
com.stackoverflow。my_package(使用下划线的Snake Case) com.stackoverflow。my-package(连字符串) com.stackoverflow.myPackage(驼色案例) com.stackoverflow.MyPackage (Pascal案例)
一般标准是什么?
当前回答
以下是官方命名规范文件规定的内容:
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
参考文献
代码约定/命名
特别要注意的是,上面的文档没有指定顶级域前缀后面的任何内容。JLS也同意这一点,并给出了以下例子:
com.sun.sunsoft.DOE gov.whitehouse.socks.mousefinder com.JavaSoft.jag.Oak org.npr.pledge.driver uk.ac.city.rugby.game
以下节选也是相关的:
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.
参考文献
包名称
其他回答
这三个都不是惯例。
使用com.stackoverflow.mypackage。
包名不遵循驼峰大小写或下划线或连字符包命名约定。
此外,谷歌Java风格指南指定了完全相同的约定(即com.stackoverflow.mypackage):
5.2.1包名 包名都是小写的,连续的单词简单地连接在一起(没有下划线)。例如,com.example.deepspace,而不是com.example.deepspace或com.example.deep_space。 —谷歌Java风格指南:5.2按标识符类型划分规则:5.2.1包名。
以下是官方命名规范文件规定的内容:
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
参考文献
代码约定/命名
特别要注意的是,上面的文档没有指定顶级域前缀后面的任何内容。JLS也同意这一点,并给出了以下例子:
com.sun.sunsoft.DOE gov.whitehouse.socks.mousefinder com.JavaSoft.jag.Oak org.npr.pledge.driver uk.ac.city.rugby.game
以下节选也是相关的:
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.
参考文献
包名称
官方的命名约定并没有那么严格,除了前缀(在你的例子中是com)之外,他们甚至没有“禁止”驼色大小写符号。
但我个人会避免大写字母和连字符,甚至数字。我会像Bragboy建议的那样选择com.stackoverflow.mypackage。
(连字符“-”在包名中是不合法的)
EDIT
有趣的是,语言规范也提到了命名约定。
在第7.7章唯一包名中,我们看到包名由大写字母组成的例子(因此骆驼式表示法是可以的),他们建议用下划线替换连字符("mary-lou" -> "mary_lou"),用下划线来前缀java关键字("com.example. example. ")。Enum " -> "com.example._enum")
更多关于包名中大写字母的例子可以在6.8.1章包名中找到。
在包名中串联单词是大多数开发人员不会做的事情。
你可以用。
com.stackoverflow.mypackage
参考JLS名称声明
包名中的下划线看起来很难看。对于三个或三个以上单词的组合,我使用首字母(例如:com.company.app.ingresoegresofijo (ingreso/egreso fijo) -> com.company.app.iefijo),然后在package-info.java中记录包的用途。