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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM