• java包名中是否可以包含下划线?


    提问:在开发的过程中经常遇这样一种情况,一个包名是由多个单词组成的,这是时候该不该用下划线分割呢?

    例如,my package,com.example.mypackage or com.example.my_package ?

    来看一下官方文档怎么说的:

    Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

    包的名称都是用小写字母写的,以避免与类或接口的名称发生冲突。

    回答:所以,一个包名由多个单词组成,全部需要小写。

    URL地址:https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

    提问:java包名中不允许出现下划线吗?

    官方是这样说的:

    Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com.

    公司使用他们的反向域名开始他们的包名-例如  域名 example.com 需要一个mypackage包,完整包名应该是 com.example.mypackage 

    但是有些情况下,域名可能不是一个有效的包名。例如包含-、包含java 关键字、数字开头等。这种情况下,官方建议我们使用下划线作为包名前缀(Package Name Prefix),但仅限于域名部分(Domain Name)。

    Naming a Package (The Java™ Tutorials > Learning the Java Language > Packages)

    Naming a Package

    命名软件包

    With programmers worldwide writing classes and interfaces using the Java programming language, it is likely that many programmers will use the same name for different types. In fact, the previous example does just that: It defines a Rectangle class when there is already a Rectangle class in the java.awt package. Still, the compiler allows both classes to have the same name if they are in different packages. The fully qualified name of each Rectangle class includes the package name. That is, the fully qualified name of the Rectangle class in the graphics package is graphics.Rectangle, and the fully qualified name of the Rectangle class in the java.awt package is java.awt.Rectangle.

    随着全世界的程序员都在使用 Java 编程语言编写类和接口,很多程序员可能会对不同的类型使用相同的名称。实际上,前面的示例就是这样做的: 当 java.awt 包中已经有一个 Rectangle 类时,它定义了一个 Rectangle 类。尽管如此,如果两个类位于不同的包中,编译器允许它们具有相同的名称。每个 Rectangle 类的完全限定名都包含包名。也就是说,图形包中的 Rectangle 类的完全限定名称是 graphics。Rectangle,而 java.awt 包中 Rectangle 类的完全限定名是 java.awt。长方形。

    This works well unless two independent programmers use the same name for their packages. What prevents this problem? Convention.

    除非两个独立的程序员对他们的软件包使用相同的名称,否则这种方法很有效。

    Naming Conventions

    命名约定

    Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

    为了避免与类或接口的名称发生冲突,包名称都是用小写写的。

    Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com.

    公司使用他们反向的互联网域名来开始他们的包名称ー例如,com.example.mypackage 用于一个名为 mypackage 的包,这个包是由 example. com 的一个程序员创建的。

    Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name (for example, com.example.region.mypackage).

    单个公司内部发生的名称冲突需要通过该公司内部的约定来处理,也许可以在公司名称后面包含区域或项目名称(例如,com.example.region.mypackage)。

    Packages in the Java language itself begin with java. or javax.

    Java 语言本身中的包以 Java.or javax 开头。

    In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int". In this event, the suggested convention is to add an underscore. For example:

    在某些情况下,internet 域名可能不是有效的包名。如果域名包含连字符或其他特殊字符,如果包名以非法用作 Java 名称开头的数字或其他字符开头,或者如果包名包含保留的 Java 关键字,如“ int”,则可能发生这种情况。在这种情况下,建议的约定是添加一个下划线。例如:

    Legalizing Package Names 软件包名称合法化
    Domain Name 域名Package Name Prefix 包名称前缀
    hyphenated-name.example.org org.example.hyphenated_name
    example.int int_.example
    123name.example.com com.example._123name
  • 相关阅读:
    python Windows环境下文件路径问题
    pycharm 取消连按两下shift出现的全局搜索
    python2 与 python3的区别
    Python安装PyOpenGL
    Protobuffer学习文档
    python bin文件处理
    python 项目自动生成requirements.txt文件
    pytest文档7-pytest-html生成html报告
    python from __future__ import division
    细说 Java 的深拷贝和浅拷贝
  • 原文地址:https://www.cnblogs.com/Chary/p/15816899.html
Copyright © 2020-2023  润新知