Java 包名称中的单词分隔符的约定是什么? [英] What is the convention for word separator in Java package names?

查看:31
本文介绍了Java 包名称中的单词分隔符的约定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包名中的一个单词应该如何分隔?以下哪些是正确的?

How should one separate words in package names? Which of the following are correct?

  1. com.stackoverflow.my_package(Snake Case 使用下划线)
  2. com.stackoverflow.my-package(Kebab Case 使用连字符)
  3. com.stackoverflow.myPackage (Camel Case)
  4. com.stackoverflow.MyPackage (Pascal Case)
  1. com.stackoverflow.my_package (Snake Case using underscore)
  2. com.stackoverflow.my-package (Kebab Case using hyphens)
  3. com.stackoverflow.myPackage (Camel Case)
  4. com.stackoverflow.MyPackage (Pascal Case)

什么是通用标准?

推荐答案

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

Here's what the official naming conventions document prescribes:

唯一包名的前缀总是用全小写的ASCII字母写的,应该是顶级域名之一,目前是comedugovmilnetorg 或标识国家的英文两字母代码之一ISO 标准 3166,1981.

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.

  • com.sun.eng
  • com.apple.quicktime.v2
  • edu.cmu.cs.bovik.cheese

参考资料

  • java.sun.com - 代码约定/命名
  • 请特别注意,顶级域前缀之后的任何内容都未在上述文档中指定.JLS 也同意这一点,举了以下例子:

    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:

    • 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:

    • 如果域名包含连字符或标识符中不允许的任何其他特殊字符,请将其转换为下划线.
    • 如果任何生成的包名称组件是关键字,则在其后添加下划线.
    • 如果任何生成的包名称组件以数字或任何其他不允许作为标识符首字符的字符开头,请在组件前加上下划线.

    参考资料

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆