在 Eclipse 中使用 Java 的默认包

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

Using Java's default package in Eclipse

javaeclipsepackage

提问by Makoto

In Eclipse, when I start a new Project, I go through the wizard, and when I get to writing my first class for that project I am asked to select a package. Sometimes out of laziness I just choose the default package.

在 Eclipse 中,当我开始一个新项目时,我会通过向导,当我开始为那个项目编写我的第一个类时,我被要求选择一个包。有时出于懒惰,我只是选择默认包。

The wizard warns me this is discouraged. Even if I ignore the warnings I never have any problems with the application due to this. Or at least, so far I have never had any problem.

向导警告我这是不鼓励的。即使我忽略了警告,我的应用程序也不会因此出现任何问题。或者至少,到目前为止我从来没有遇到过任何问题。

So why does Eclipse want me to create a new package?

那么为什么 Eclipse 要我创建一个新包呢?

回答by Makoto

Eclipse, and most other IDEs, are geared towards large projects. Hobbyist programming and small-scale assignments can get by in IDEs often, but be aware that the general assumption would be for larger projects - anything between 10 and 5,000+ classes.

Eclipse 和大多数其他 IDE 面向大型项目。业余爱好者编程和小规模作业通常可以在 IDE 中完成,但请注意,一般假设适用于较大的项目 - 10 到 5,000 多个类之间的任何项目。

There is also a chance that you create a class which has a similar name to something in the Java API - for example:

您也有可能创建一个与 Java API 中名称相似的类 - 例如:

Ambiguity in instantiating the class (throw new MarshalException();) if both classes exist on the same classpath is a compilation error.

throw new MarshalException();如果两个类都存在于同一类路径中,则实例化类 ( ) 时的歧义是编译错误。

回答by Jason Dunkelberger

You actually can't import classes from the default package. I understand it's allowed as a convenience or for very small (one class file) tasks.

您实际上无法从默认包中导入类。我知道这是为了方便或非常小的(一个类文件)任务而被允许的。