Java code convention for package names [duplicate]

左心房为你撑大大i 提交于 2019-12-22 04:18:15

问题


Possible Duplicate:
what is the convention for word separator in java package names?

I wonder if there are some code convension for package name which contains several words. E.g. package name com.dreamcom.objectInterfaces. Is camel case suitable in this case?


回答1:


From Java package naming conventions @ Wikipedia (emphasis added):

Packages are usually defined using a hierarchical naming pattern, with levels in the hierarchy separated by periods (.) (pronounced "dot"). Although packages lower in the naming hierarchy are often referred to as "subpackages" of the corresponding packages higher in the hierarchy, there is almost no semantic relationship between packages. The Java Language Specification establishes package naming conventions to avoid the possibility of two published packages having the same name. The naming conventions describe how to create unique package names, so that packages that are widely distributed will have unique namespaces. This allows packages to be separately, easily and automatically installed and catalogued.

In general, a package name begins with the top level domain name of the organization and then the organization's domain and then any subdomains, listed in reverse order. The organization can then choose a specific name for its package. Package names should be all lowercase characters whenever possible.

For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package ca.mysoft.fractions distinguishes the fractions package from another similar package created by another company. If a German company named MySoft also creates a fractions package, but names it de.mysoft.fractions, then the classes in these two packages are defined in a unique and separate namespace.

Complete conventions for disambiguating package names and rules for naming packages when the Internet domain name cannot be directly used as a package name are described in section 7.7 of the Java Language Specification.


See also:

  • The Java Language Specification §7.7 Unique Package Names
  • Do you really use your reverse domain for package naming in java?



回答2:


The convention is to use lower case throughout.

More information is available in the JLS here.




回答3:


Camel-casing package names is not something I like. With multiple words in a package name, I consider the following:

  1. Isn't "com.dreamcom.interfaces" good enough?

  2. Will "com.dreamcom.object.interfaces" work?

If not, then rather rather consider "com.dreamcom.objectinterfaces" than camel casing.

The reason for this is that package names mirrors directories on the host OS. On Linux "com.dreamcom.objectInterfaces" and "com.dreamcom.objectinterfaces" are distinct, while on Windows they are the same.



来源:https://stackoverflow.com/questions/8243316/java-code-convention-for-package-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!