有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

如何在软件包名称中使用单独的单词?以下哪项是正确的

  1. com.stackoverflow.my_packageSnake Case使用下划线)
  2. com.stackoverflow.my-packageKebab Case使用连字符)
  3. com.stackoverflow.myPackageCamel Case
  4. com.stackoverflow.MyPackagePascal Case

一般标准是什么


共 (6) 个答案

  1. # 1 楼答案

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

    任何人都不应该使用hypen-(这是一种糟糕的做法)

    任何人都不应该在包裹名称中使用大写字母(糟糕的做法)

    注意:这里的“坏习惯”是指从技术上来说,你可以使用它,但按照惯例,它不是一种好的写作方式

    资料来源:Naming a Package(docs.oracle)

  2. # 2 楼答案

    官方的命名约定没有那么严格,它们甚至不“禁止”除前缀以外的大小写符号(com

    但我个人会避免大写字母和连字号,甚至是数字。我也会像布拉格博建议的那样选择com.stackoverflow.mypackage

    (连字符“-”在程序包名称中不合法)

    编辑

    有趣的是,语言规范中也提到了命名约定

    Chapter 7.7 Unique Package Names中,我们看到了一些示例,其中包名由大写字母组成(因此CamelCase表示法可以),他们建议用下划线(“mary lou”->;“mary_lou”)替换连字符,并用下划线(“com.example.enum”->;“com.example._enum”)作为java关键字的前缀

    在第6.8.1 Package Names章中可以找到更多关于包名中大写字母的示例

  3. # 3 楼答案

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

    你可以用类似的东西

    com.stackoverflow.mypackage

    参考JLS Name Declaration

  4. # 4 楼答案

    下划线在包名中看起来很难看。值得一提的是,如果名称由三个或更多单词组成,我会使用首字母缩写(例如:com.company.app.ingresoegresofijo (ingreso/egreso fijo) -> com.company.app.iefijo),然后在package-info.java中记录文件包的用途

  5. # 5 楼答案

    这三个都不是惯例

    使用com.stackoverflow.mypackage

    包名不遵循驼峰大小写或下划线或连字符package naming 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, not com.example.deepSpace or com.example.deep_space.

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

  6. # 6 楼答案

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

    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.

    参考文献