English 中文(简体)
为什么 Java的来源相互之间有这么多的夹子?
原标题:Why do Java sources have so many folders inside each other?
  • 时间:2012-01-13 15:09:04
  •  标签:
  • java

每次我看看 Java源代码时,我都发现自己是双倍的,有双倍的足迹。 为什么 Java需要如此多的nes子,除新的子夹外,他们别无其他东西?

For example: https://github.com/halfninja/android-dragcontrol3d/tree/master/src/uk/co/halfninja/android That s probably not the worst example, but there are two folders "uk" and "co" that just don t make sense. I see this in Java sources only!

http://www.ludumdare.com/compo/ludum-dare-22/?action=preview&uid=398”rel=“noretinger” http://www.ludumdare.com/compo/ludum-dare-22/?action=preview&uid=398。

import com.mojang.ld22.gfx.Font;
import com.mojang.ld22.gfx.Screen;
import com.mojang.ld22.gfx.SpriteSheet;

为什么不只字:

import gfx.Font;
import gfx.Screen;
import gfx.SpriteSheet;

这更加清洁。

(我从未在 Java策划过)

最佳回答

这样做是为了防止与其他支柱的冲突。 如同公司在包裹名称中的“url”一样,这很可能是独一无二的,不能与他人的包裹和班子相冲突。

Your example is a good one, since it seems pretty reasonable to imagine two people thinking of using "gfx" as a package name and with classes like Font or Sprite. Now, if you wanted to use both of them, how could you since the package and class name would be the name?

问题回答

Your way is cleaner, but it assumes nobody else in the world is ever going to create a package called gfx, which is a pretty weak assumption. By prepending your reversed domain name, you create a unique namespace that avoids collisions.

这与瓦 Java方案拟定过程中的“共享文化”完全吻合,在这些应用中,许多来源的大型图书馆通常会合在一起。

在 Java,公约将指明贵组织(通常包括TLD和公司名称)和项目(可能增加几个部分)的资料(与载有你的代码的组合结构相对应)。

更具体地说,这也减少了偶然地与对方串通点名空间的可能性。

它只是防止名称空间冲突的组织技术。 至少是这样。 贾瓦一揽子方案的名称与基本目录结构相符,因此,一揽子方案一级的任何组织模式都将予以反映。 工作队以其组织的名称和特殊轴心,开始其一揽子名称,是典型的。 这只是公约,但是它根深蒂固,在没有非常好的理由的情况下应当遵循。

它涉及Namespaces。 有了名称空间,你可以设立2个名称相同的班级,位于不同的包装/夹。 这种名称逻辑也可用于设定准入特权等。 以下是一些联系:

1) Namespace 2) Java Package 3) Java Package Naming Conventions

EDIT: Let us assume that you are creating a new project and are using 2 open source frameworks from companies/organizations - comA and comB. Also, let us assume that comA and comB have created a class in their projects with the same classname. Now, with the Java package naming conventions, we have com.comA.SomeClass and com.comB.SomeClass. You can import and use both the classes in your class, without having a conflict. This is just a simple example. There are other uses from this naming convention.

如果你想要与所有其他人分享法典,但使用通用名称而不发生冲突。 认为列入您域名(背局)的良好做法

Everyone write a package called gfx.Font you wouldn t be able to use more than one version in the same application.

你们可能感到,你们的法典不会与世界共享(甚至不应分享)。 在此情况下,简便的一揽子结构可能更为简单。

如果你使用国际民主和选举援助学会,它就能够很好地隐藏长期一揽子结构,因此你无需担心。

This is due to recommended packaging structure. In large projects, so many packages/libraries are used and in order not to put source files into same folder with another library, programmers put their source codes into unique folders. As websites are unique, it is a convention to use packaging structure that looks like folder structure of websites.

Java does not require anything: you can just put all your classes in the default package and surf away. But for serious projects that kind of organization is not only wise, it s mandatory. The com.mojang.ld22 part is just a convention:

  • com = either this or org, java/javax for official packages
  • mojang = second part is company name
  • ld22 = third part is application name




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签