1

我相信我们都知道,可以通过购买像“somedomain.org”这样的域名来为其Java产品命名包,在这种情况下,包将遵循“org.somedomain.packagename”模式(相关来源:http ://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html)。

但是,对于那些没有域名的人,电子邮件地址作为替代提供。例如,一个名叫 John Bob Doe 的人可能创建了一个 GMail 帐户,例如“john_bob_doe@gmail.com”,因此会将他们的包命名为:“com.gmail.john_bob_doe”和“com.gmail.jon_bob_doe”。一些内包”

这一切都很好,但如果他的电子邮件地址是“john-bob-doe@gmail.com”会发生什么?好吧,根据上面的链接,在存在连字符的情况下,我们可以用下划线替换连字符。因此,再次:“com.gmail.john_bob_doe”和“com.gmail.jon_bob_doe.someinnerpackage”。

现在这是“真正的”问题:如果他的电子邮件地址是“john.bob.doe@gmail.com”会发生什么?那么包命名应该是“com.gmail.john.bob.doe”吗?或者也许是“com.gmail.doe.bob.john”?基本上,问题是:当他们的电子邮件地址在“@”符号之前包含句点时,应该如何命名他们的包?

推理:我有一个这样的电子邮件地址,我想用它来命名我的项目的包。

4

2 回答 2

4

Please note that naming conventions of Java packages are just that; conventions. You can name them whatever you want. The only thing that is enforced is that a . delineates a subdirectory in the filesystem where the compiler looks for the source.

Also note that with gmail, at least, the . is ignored in the email. Therefore myname@gmail.com and my.name@gmail.com are equivalent. Because they are equivalent, com.gmail.myname would be an appropriate package name tied to such an email address.

The reason for using domain names/email addresses is that they are a convenient way to (probably) avoid namespace collisions for packages. However, with modern refactoring technology available in your local IDE, this should not be a blocker for progress. Choose a non-top-level-domain prefix and name your program or library whatever you want rogue.my.awesome.java.program.

Finally, note that naming your package something like com.google.myname implies that google developed the package. Most programmers, for this reason, put some amount of work into establishing some screen name or shortening of their actual name as an online presence so that they can use that as a prefix for this and similar projects: myname.awesome.java.program. This satisfies an elegant secondary concern of naming: knowing who to contact if, upon coming across the source, you have questions.

于 2013-04-09T17:46:19.420 回答
2

没有单一的正确方法可以做到这一点。

您需要牢记的是这个约定/包名称形成指南的目的......这是:

  • 最大限度地减少相互独立开发的 Java 代码库之间意外包名称冲突的风险,以及

  • 传达有关谁或什么“拥有”您正在创建的命名空间的一些信息。

从这个角度来看com.gmail.john.bob.doe,还是com.gmail.doe.bob.john同样可以很好地将碰撞风险降到最低,并且有最小的风险,聪明的人会认为com.gmail(即谷歌)拥有命名空间。


使用下划线违反了一般的 Java 标识符命名约定......我建议不要这样做

于 2013-04-09T17:55:35.210 回答