eclipse 我不明白 src 文件夹和单独包的用途

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3225663/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:53:10  来源:igfitidea点击:

I don't understand the purpose of the src folder and seperate packages

javaeclipse

提问by Bibendum

I've been using eclipse only for python over the last few months, and I'd like to start using it for java. However, according to the tutorials I've looked at, the proper way to organize your java project is to create a package in the source folder named, for example, com.project, and have all the classes and such be named com.project.class. You can also make sub-packages that work similar to sub-directories such as com.project.utilities.*. With this convention, I don't see why I would create more than one package per project. Since all the code is contained within this structure, what purpose does the src folder serve?

在过去的几个月里,我一直只将 eclipse 用于 python,我想开始将它用于 java。但是,根据我看过的教程,组织 Java 项目的正确方法是在源文件夹中创建一个包,例如,命名为com.project,并将所有类等命名为com.project.class。您还可以制作类似于子目录的子包,例如com.project.utilities.*. 有了这个约定,我不明白为什么我要为每个项目创建多个包。既然所有的代码都包含在这个结构中,那么 src 文件夹有什么作用呢?

I hope I'm just wrong about this being the normal way to structure a java project, because it seems pretty inconvenient.

我希望我只是错误地认为这是构建 Java 项目的正常方式,因为它看起来很不方便。

Also, I haven't fooled with this yet, but wouldn't this make loading external dependencies a pain? If I have an img folder placed next to the src and bin folders, wouldn't I have to use "..\img*" to access it?

另外,我还没有被这个愚弄,但这不会使加载外部依赖项变得痛苦吗?如果我在 src 和 bin 文件夹旁边放置了一个 img 文件夹,我是否不必使用“..\img*”来访问它?

回答by OscarRyz

Yes, for small project might not make much sense. You could just have:

是的,对于小项目可能没有多大意义。你可以只拥有:

MyProject
|
+ - FileOne.java
+ - FileTwo.java
+ - FileThree.java

But for larger projects you may need to separate into packages, classes that belong to different kinds of functionality.

但是对于较大的项目,您可能需要将属于不同类型功能的包、类分开。

For instance the core java library has ( to name a few )

例如,核心 java 库有(仅举几例)

java.lang ( contains core clases such as Object, String, Integer, Boolean, StringBuilder ) java.util ( contains utility classes like List, ArrayList, Date, Map, Timer etc ) java.io ( contains classes for Input/Ouput like File, InputStreamReader, BufferedReader etc

java.lang(包含核心类,如 Object、String、Integer、Boolean、StringBuilder) java.util(包含实用类,如 List、ArrayList、Date、Map、Timer 等) java.io(包含输入/输出类,如 File , InputStreamReader, BufferedReader 等

java.sql, java.swing, java.text etc. etc

java.sql、java.swing、java.text 等

That way, you "pack together" classes that are related to each other.

这样,您就可以“打包”彼此相关的类。

The source code for these classes, are by convention in a folder named src

这些类的源代码按照约定位于名为 src

So you would have:

所以你会有:

YourProject 
|
+ - src 
     |
     + packageA
     |
     + packageB

You may also need to separate source code from compiled files, so the classesfolder is used by convention. Additionally you may want a separate folder to put 3rd part libraries in, another for resources like images, auxiliary files or other, a different for documentation, etc.

您可能还需要将源代码与编译文件分开,因此classes按惯例使用该文件夹。此外,您可能需要一个单独的文件夹来放置第三部分库,另一个用于放置图像、辅助文件或其他资源,另一个用于文档等。

So a typical layout may be:

所以一个典型的布局可能是:

YourProject
|
+ - src/ 
+ - lib/
+ - classes/
+ - resources/ 
+ - conf/ 
+ - bin/
+ - doc/
+ - etc/

But of course, it only makes sense for large projects.

但当然,它只对大型项目有意义。

Web apps usually contain also a WEB-INF folder etc.

Web 应用程序通常还包含一个 WEB-INF 文件夹等。

If your project contains only a couple of classes, don't worry and go with a single folder, but it good to know what's the rationale.

如果您的项目仅包含几个类,请不要担心并使用单个文件夹,但很高兴知道其基本原理是什么。

回答by dbyrne

I can understand why this might seem inconvenient for small projects. However, if you ever have to work on a project with hundreds (or thousands) of source files, having intuitive sub-packages are an absolute necessity to keep everything organized.

我可以理解为什么这对于小型项目来说似乎不方便。但是,如果您不得不处理包含数百(或数千)个源文件的项目,那么拥有直观的子包对于保持一切井井有条是绝对必要的。

As far as loading external dependencies based on the relative path from where the source file is located, it all depends on how the compiled application is organized. It is not typical to reference resources with "..\img" like you describe.

至于根据源文件所在位置的相对路径加载外部依赖项,这完全取决于编译后的应用程序的组织方式。像您描述的那样使用“..\img”来引用资源并不常见。

回答by Todd Owen

You don't haveto put everything in a package. For very small apps you can just put the java source file(s) directly in the src directory, which means your classes will belong to the "default" package. But most projects use a unique package name in order to avoid name clashes, for example between java.util.Date and java.sql.Date.

你不必把一切都在一个包。对于非常小的应用程序,您可以将 java 源文件直接放在 src 目录中,这意味着您的类将属于“默认”包。但是大多数项目使用唯一的包名以避免名称冲突,例如在 java.util.Date 和 java.sql.Date 之间。

Using a "src" directory is a convention that the IDE, build tools, and other programmers all readily understand. If you avoided "src" and instead used a directory structure like:

使用“src”目录是IDE、构建工具和其他程序员都容易理解的约定。如果您避免使用“src”而是使用如下目录结构:

com/project/MyClass.java
img/icon.jpg
...etc

Then you can't simply tell the IDE to look for java source files in the "com" folder, because then it will interpret the class as being "project.MyClass" instead of "com.project.MyClass". On the other hand, you can't tell it to look for source files in the root folder either, because then it will expect the img folder to contain java sources.

然后您不能简单地告诉 IDE 在“com”文件夹中查找 java 源文件,因为它会将类解释为“project.MyClass”而不是“com.project.MyClass”。另一方面,您也不能告诉它在根文件夹中查找源文件,因为它会期望 img 文件夹包含 java 源。