java 反向域名对java包结构有什么意义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2475168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the significance of the reverse domain name for java package structure
提问by Vaishak Suresh
Why do we use reverse domain name like com.something. or org.something. structure for java packages? I understand this brings in some sort of uniqueness, but why do we need this uniqueness?
为什么我们使用像 com.something 这样的反向域名。或 org.something。java包的结构?我知道这会带来某种独特性,但为什么我们需要这种独特性?
采纳答案by Laurence Gonsalves
Globally unique package names avoid naming collisions between libraries from different sources. Rather than creating a new central database of global names, the domain name registry is used. From the JLS:
全局唯一的包名避免了来自不同来源的库之间的命名冲突。不是创建一个新的全球名称中央数据库,而是使用域名注册。来自 JLS:
The suggested convention for generating unique package names is merely a way to piggyback a package naming convention on top of an existing, widely known unique name registry instead of having to create a separate registry for package names.
生成唯一包名称的建议约定仅仅是在现有的、广为人知的唯一名称注册表之上搭载包命名约定的一种方式,而不必为包名称创建单独的注册表。
回答by Claudiu
About why we do it reversed: Imagine you have two important packages, an accounting package and a graphics package. If you specified these in 'straight' order:
关于我们为什么要这样做:假设您有两个重要的包,一个会计包和一个图形包。如果您以“直接”顺序指定这些:
accounting.mycompany.org
graphics.mycompany.org
Then it implies there is a major accountingpackage, a subsection of which is for mycompany, and a subsection of thatpackage is called the orgpackage which you actually use. However, you want this:
然后它意味着有一个主要accounting包,其中的一个小节是 for mycompany,并且该包的一个小节称为org您实际使用的包。但是,你想要这个:
org.mycompany.accounting
org.mycompany.graphics
This makes more sense. Out of all packages from organizations (org), you look at mycompanyin particular, and it has two sub-packages, the accountingand the graphicsones.
这更有意义。在来自组织 ( org)的所有包中,您mycompany特别注意,它有两个子包,theaccounting和 the graphics。
回答by saugata
As you say, reverse domain names as base package name ensures uniqueness. Suppose two companies with DN example.com and example.org both define the class Employee in their framework. Now if you are using both frameworks you will not be able pinpoint which Employee you want to use in your code, but if they are defined in packages com.example and org.example respectively you can tell the compiler/JVM specifically which class you are referring to. If unique packages are not defined you will get compilation errors or runtime errors, e.g. if you are using the com employee class, but the org employee class gets loaded first from the classpath you will get a runtime error, since the two employee classes may not have same structure.
正如您所说,反向域名作为基本包名称可确保唯一性。假设 DN 为 example.com 和 example.org 的两家公司都在其框架中定义了类 Employee。现在,如果您同时使用这两个框架,您将无法确定您想在代码中使用哪个 Employee,但是如果它们分别在包 com.example 和 org.example 中定义,您可以具体告诉编译器/JVM 您是哪个类指。如果未定义唯一包,您将收到编译错误或运行时错误,例如,如果您使用的是 com 员工类,但 org 员工类首先从类路径加载,您将收到运行时错误,因为这两个员工类可能不会具有相同的结构。
回答by Padmarag
The uniqueness is needed for Class Loading.
It helps by avoiding naming collisions. If there are classes with same package name and class name, Collision will occur while trying to load the classes.
This generally happens if there are multiple libraries(jar) that contain classes with same names.
Also see this.
类加载需要唯一性。
它有助于避免命名冲突。如果存在具有相同包名和类名的类,则在尝试加载类时会发生冲突。
如果有多个包含同名类的库(jar),通常会发生这种情况。
也看到这个。
回答by Stephen C
You need the uniqueness if you might need to integrate your code with third party software, or provide it to someone else for integration. If you don't follow the rules, you increase the risk that at some point you will have a class naming collision, and that you will need to rename lots of your classes to address it. Or worse still, that your customers will have to do the code renaming.
如果您可能需要将代码与第三方软件集成,或将其提供给其他人进行集成,则您需要唯一性。如果您不遵守规则,就会增加在某些时候发生类命名冲突的风险,并且您将需要重命名许多类来解决它。或者更糟糕的是,您的客户将不得不进行代码重命名。
This also applies when code is produces as part of different projects in an organization.
这也适用于作为组织中不同项目的一部分生成代码的情况。
回答by Kim L
As you said, it brings uniqueness, something that is needed especially when working with third party code. For example, consider that you are using a library I've made. I've used the package "foo" and have a class named Bar there. Now if you are also using the package name "foo" AND you have a class named Bar, this would mean that your implementation would override my Bar implementation, making my implementation inaccessible. On the other hand, if my package were "com.mydomain.foo" and I'd had by Bar class there, then you can freely use the name Bar in one of your classes and both classes could still be uniquely identified and used separately.
正如您所说,它带来了独特性,这是在使用第三方代码时尤其需要的东西。例如,假设您正在使用我制作的库。我使用了包“foo”,并在那里有一个名为 Bar 的类。现在,如果您还使用包名称“foo”并且您有一个名为 Bar 的类,这意味着您的实现将覆盖我的 Bar 实现,使我的实现无法访问。另一方面,如果我的包是“com.mydomain.foo”并且我在那里有 Bar 类,那么您可以在其中一个类中自由使用名称 Bar,并且两个类仍然可以唯一标识并单独使用.
Why use the reverse domain name as the package name? I guess that is just a convention to make sure that everybody uses a unique namespace, as you shouldn't use someone else's domain in your package name.
为什么要使用反向域名作为包名?我想这只是一个约定,以确保每个人都使用唯一的命名空间,因为您不应该在包名称中使用其他人的域。

