Java包名称中的单词分隔符的约定是什么?

What is the convention for word separator in Java package names?

包名称中应该如何单独使用一个词?以下哪项是正确的?

  • com.stackoverflow.my_package(下划线)
  • com.stackoverflow.my-package(连字符)
  • com.stackoverflow.MyPackage(骆驼壳)
  • 一般标准是什么?


    以下是官方命名约定文件规定的:

    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

    工具书类

    • java.sun.com-代码约定/命名

    注意,特别是,上面的文档没有指定顶级域前缀后面的任何内容。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.

    工具书类

    • JLS 6.8.1包装名称
    • JLS 7.7唯一的软件包名称


    这三个都不是惯例。

    使用com.stackoverflow.mypackage

    包名称不遵循camel大小写、下划线或连字符包命名约定。

    此外,谷歌Java风格指南完全相同(即EDCOX1(0))约定:

    5.2.1 Package names

    Package names are all lowercase, with consecutive words simply concatenated together (no underscores). For example, com.example.deepspace, not com.example.deepSpace or com.example.deep_space.

    — Google Java Style Guide: 5.2 Rules by identifier type: 5.2.1 Package names.


    官方的命名约定并不那么严格,它们甚至没有"禁止"骆驼大小写符号,除了前缀(在您的示例中是com)。

    但我个人会避免大写字母和连字符,偶数。我也会像布拉格男孩建议的那样选择com.stackoverflow.mypackage

    (连字符"-"在包名称中不合法)

    编辑

    有趣的是,语言规范也有一些关于命名约定的内容。

    在第7.7章独特的包名称中,我们看到了包含大写字母的包名的例子(因此CamelCase符号可以),他们建议用下划线("玛丽娄"->"MayyLuo")替换前缀,并用下划线("COM.Simult.EnUM"->"com .Simul..EnEUM")前缀Java关键字。

    在第6.8.1章包名称中可以找到包名称中大写字母的更多示例。


    任何人都可以使用下划线(没关系)

    任何人都不应该使用催眠术(它的坏做法)

    包装名称中不得使用大写字母(不好的做法)

    ></P><P>来源:命名软件包(docs.oracle)</P></p>
<div class=


    包名称中的单词串联是大多数开发人员不做的事情。

    你可以用类似的东西。

    com.stackoverflow.mypackage

    参考JLS名称声明


    包名称中的下划线看起来很难看。就其价值而言,如果名称是由三个或更多单词组成的复合词,我使用首字母(例如:com.company.app.ingresoegresofijo (ingreso/egreso fijo) -> com.company.app.iefijo),然后在package-info.java中记录包装用途。