java中包名是如何确定的,为什么要用“com”?作为根?
我尝试用谷歌搜索这个但找不到任何东西。我只是好奇为什么我们使用文件夹名称“com”作为根目录?它代表什么?电脑?哈哈。谢谢
I tried googling this but can't find anything. I'm just curious as to why we use the folder name 'com' as the root directory? what does it stand for? computer? haha. thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了避免冲突,命名包时的标准做法是按照您拥有的域的顺序命名它们。因此,如果您公司的网站是
betterwidgets.com
,您几乎肯定会将文件放入com.betterwidgets.*
包中。Java(可能还有其他语言)规定目录结构需要符合包层次结构(即使它是非层次结构的,请看图),因此在这种情况下,所有源文件都将位于“com”目录下,然后是其中的“betterwidgets”子目录。
Standard practice when naming packages, in order to avoid collisions, is to name them after a domain that you own, in reverse order. So if your company's website was
betterwidgets.com
, you would almost certainly put your files in acom.betterwidgets.*
package.Java (and probably other languages) impose that the directory structure needs to conform to the package hierarchy (even though it's non-hierarchical, go figure) and so in this case, all the source files would be under "com" directory, then a "betterwidgets" subdirectory within that.
在java中,约定是用包提供者的域名的倒数作为前缀来命名包。例如,由 microsoft(拥有“microsoft.com”)开发的包“foobar”可能被命名为“com.microsoft.foobar”。如果您的公司拥有域“fantastic.net”,您可以将该包命名为“net.fantastic.foobar”。
这些包在磁盘上按照其名称进行组织,因此“net.fantastic.foobar”将位于“com/fantastic/foobar”文件夹中。
这只是一种简单、低开销的方法,可以防止两家公司开发同名的软件包。
FWIW,域名上下文中的“com”代表“商业”,如“与商业相关”。如果您好奇的话,维基百科有更多信息。
In java, the convention is to name packages prefixed by the reverse of the domain name of the package provider. For example, the package "foobar" developed by microsoft (which owns "microsoft.com") might be named "com.microsoft.foobar". If your company owns the domain "fantastic.net", you would name the package "net.fantastic.foobar".
The packages are organized on disk related to their name, so "net.fantastic.foobar" would be in the folder "com/fantastic/foobar".
This is just a simple, low-overhead way to prevent two companies from developing packages that have the same name.
FWIW, the "com" in a domain name context stands for "commercial", as in "related to commerce". Wikipedia has more information if you are curious.
它是一个域名,其组成部分按相反顺序排列。
It's a domain name with the components in reverse order.
通常“.com”代表商业解决方案。在本例中,我认为该包是商业 API 的一部分。
Generally ".com" stands for a commercial solution. In this case I suppose that the package is part of a commercial API.