简体   繁体   English

在Eclipse中使用Java的默认包

[英]Using Java's default package in Eclipse

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. 在Eclipse中,当我启动一个新项目时,我会通过向导,当我为该项目编写第一个类时,我被要求选择一个包。 Sometimes out of laziness I just choose the default package. 有时出于懒惰,我只选择默认包。

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要我创建一个新包呢?

Eclipse, and most other IDEs, are geared towards large projects. Eclipse和大多数其他IDE都面向大型项目。 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. 业余爱好者编程和小规模作业通常可以在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(); )如果两个类存在于同一个类路径中则是编译错误。

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. 我知道这是一个方便或非常小(一个类文件)的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM