Java为什么要定义包名称? [英] Why does Java want package names defined?

查看:150
本文介绍了Java为什么要定义包名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Java不能根据文件夹结构弄清楚呢?

Why can't Java figure it out based on the folder structure?

似乎到包的映射已经由根源文件夹以及指向该特定文件的路径指定了.

It seems that the mapping to packages is already specified by the root source folder plus the path to that particular file.

它是完全耦合的,并且在没有IDE的情况下进行重构绝对是乏味的-尽管无论如何都会更新对该文件的引用,但是至少可以由编译器找出一部分,而不是在文件中指定包级别.

It is completely coupled, and doing a refactor without an IDE is absolutely tedious - although updating the references to that file would be anyway, but it could at least partially be figured out by the compiler rather than specifying the package at the file level.

推荐答案

来自

每个主机系统都确定如何创建和存储包和编译单元.

Each host system determines how packages and compilation units are created and stored.

每个主机系统还确定在特定编译中可以观察到哪些编译单元(第7.3节).编译单元的可观察性进而决定了哪些包是可观察的,以及哪些包在范围内.

Each host system also determines which compilation units are observable (§7.3) in a particular compilation. The observability of compilation units in turn determines which packages are observable, and which packages are in scope.

在Java SE平台的简单实现中,程序包和编译单元可以存储在本地文件系统中.其他实现可能会使用分布式文件系统或某种形式的数据库来存储它们.

如果主机系统在数据库中存储程序包和编译单元,则数据库不得对基于文件的实现中允许的编译单元施加可选限制(第7.6节).

If a host system stores packages and compilation units in a database, then the database must not impose the optional restrictions (§7.6) on compilation units permissible in file-based implementations.

作为在文件系统中存储软件包的极其简单的示例,项目中的所有软件包以及源代码和二进制代码都可以存储在单个目录及其子目录中.该目录的每个直接子目录都代表一个顶级程序包,即,其完全限定名称由一个简单名称组成.子目录的每个更高级别将代表包含目录所表示的包的子包,依此类推.

As an extremely simple example of storing packages in a file system, all the packages and source and binary code in a project might be stored in a single directory and its subdirectories. Each immediate subdirectory of this directory would represent a top level package, that is, one whose fully qualified name consists of a single simple name. Each further level of subdirectory would represent a subpackage of the package represented by the containing directory, and so on.

还请阅读有关编译单元,与您的问题密切相关.

Also read about Compilation Units , closely related to your question.

注意:从命令行进行编译时,默认情况下,每个类都将与相应的源文件放在相同的位置,但是如果使用"-d"选项,则编译器将建立适当的输出目录.

Note: When you compiling from the command line, by default each class will be put in the same location as the corresponding source file, but if you use the "-d" option the compiler will build the appropriate output directory.

这篇关于Java为什么要定义包名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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