Java中包和目录的区别 [英] Difference between Package and Directory in Java

查看:793
本文介绍了Java中包和目录的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java Project 中,将所有 .java 文件保存在同一个文件夹中是否意味着它们位于同一个包中?

In a Java Project, does keeping all the .java files in the same folder mean they are in the same package?

与将所有项目文件保存在一个文件夹中相比,为我们的项目制作一个包有什么区别?

What is the difference in making a Package for our project compared to keeping all the project files in one folder?

此主题并未真正解决我的问题。

This thread doesn't really address my question.

推荐答案

包和目录之间存在关系,但必须维护。如果你的类在mypackage1.mypackage2中,这意味着 java 命令将在名为mypackage1 \ mypackage2的目录结构中找到它。 (假设向后Windows表示法),该目录结构进一步嵌入目录(让我们称之为myjava),其名称在 classpath (或者直接在当前目录)。

There is a relationship between package and directory, but it's one that you must maintain. If you have a class that's in "mypackage1.mypackage2", that means that the java command is going to expect to find it in a directory structure named "mypackage1\mypackage2" (assuming "backwards" Windows notation), with that directory structure further embedded in a directory (let's call it "myjava") whose name is in the classpath (or else is directly in the "current directory").

所以你的Java类(在内部说 package mypackage1.mypackage2; )就在\中Users\myName\myjava\mypackage1\mypackage2 \,并在类路径中放置\ Users \ myName \ myjava,否则您将当前目录设置为\ Users \\ \\ myName\myjava。

So your Java class (which internally says package mypackage1.mypackage2;) is in, say, "\Users\myName\myjava\mypackage1\mypackage2\", and you place "\Users\myName\myjava" in the class path, or else you have your current directory set to "\Users\myName\myjava".

如果你混淆了这一点,要么根本找不到课程,要么你会得到一个类似于永远模糊的错误NoClassDefFoundError错误。

If you mix this up, either the class will not be found at all, or you will get an error something like the ever-nebulous "NoClassDefFoundError".

至于为什么会使用包(和目录),原因与名称空间和关注点分离有关(看看那些)。如果没有包并且所有java.lang,java.io,sun.misc等类都在一起,那么Java将更难保持直线。首先,必须使用名称前缀来保持它们的直线并避免名称冲突。很多逻辑分组都会丢失。

As to why one would use packages (and directories), the reason has to do with "name space" and "separation of concerns" (look those up). Java would be much harder to keep straight if there were no packages and all the "java.lang", "java.io", "sun.misc", et al classes were together. First off, one would have to use name "prefixes" to keep them straight and avoiding name conflicts. And much of the logical grouping would be lost.

使用自己的项目,你不需要为自己编写的简单小程序使用软件包,但是如果你写的话你可能会给别人一些礼貌地使用诸如myname.myproject之类的包(当然代替你的名字和项目),所以你给它的人可以将它与其他人合并而不会出现姓名冲突。

With your own projects you don't need to use packages for simple little programs you write for yourself, but if you write something you might give to someone else it's polite to use a package such as "myname.myproject" (substituting your name and project of course), so the person you give it to can combine it with others without name conflicts.

在大型应用程序中,您会发现使用更高级别的分离可以帮助您保持功能正常,因此您可以知道所有内容。它也不鼓励你在不同的功能区域之间跨越边界,所以你不会让不相关的逻辑被缠绕。

In large applications you'll find using further levels of separation helps you keep the functions straight, so you know where everything is. It also discourages you from "crossing the boundary" between different functional areas, so you don't get unrelated logic entertwined.

Eclipse(如果你使用的话)那种混乱问题有点因为它想要提供目录和包名称,有时(但并不总是)保持它们同步。

Eclipse (if you use that) kind of muddles the issue a bit because it "wants" to provide directory and package names and will sometimes (but not always) keep them in sync.

这篇关于Java中包和目录的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆