简体   繁体   English

如何从Eclipse中的包中导入类?

[英]How do I import classes from a package in Eclipse?

I have a package folder called snake with .java files and I am trying to import a specific class from it for example Move . 我有一个名为snake的软件包文件夹,带有.java文件,并且我尝试从中导入特定的类,例如Move

I have placed the folder into the same project folder as the src folder I am currently working with, but when I tried to do an import snake.Move; 我已经将该文件夹放置在与当前正在使用的src文件夹相同的项目文件夹中,但是当我尝试执行import snake.Move; I get an error message saying that the import cannot be resolved . 我收到一条错误消息,提示无法解决导入

Did I store the folder in the wrong directory? 我是否将文件夹存储在错误的目录中? Or am I missing a step? 还是我错过了一步?

Make sure your .java files are registered in a package that has the same name as the folder you use inside the src Eclipse project folder. 确保.java文件注册在与src Eclipse项目文件夹中使用的文件夹同名的软件包中。

In other words, they have to start with: 换句话说,他们必须从以下内容开始:

package snake;

.. if they are located in the src/snake folder. ..如果它们位于src/snake文件夹中。

Your source files need to be in what Eclipse calls a Source Folder . 您的源文件必须位于Eclipse所谓的“ Source Folder By default, for a new java project, src is created and marked as source folder. 默认情况下,对于新的Java项目,将创建src并将其标记为源文件夹。 If you've just pasted the folder with your sources from the filesystem into the project folder, you will see that in Eclipse, src and you folder look differently ( src has some brown blocks in it, marking it as a source folder). 如果您刚刚将带有源文件的文件夹从文件系统粘贴到项目文件夹中,则将在Eclipse中看到src并且您的文件夹看起来有所不同( src有一些棕色块,将其标记为源文件夹)。 You need to register your folder as a source folder as well. 您还需要将文件夹注册为源文件夹。

You have 2 methods to set source folders: 您有2种方法来设置源文件夹:

  1. Right click on your project and select New > Source Folder and enter the name of your simple folder. 右键单击项目,然后选择“ New > Source Folder然后输入简单文件夹的名称。 Once you click Finish, you will see your regular folder be marked just like src is and properly registered as a source folder. 单击“完成”后,您将看到常规文件夹已标记为src ,并且已正确注册为源文件夹。
  2. Right click on your project and select Properties then select the tab Java Build Path and the first sub-tab Sources . 右键单击您的项目,然后选择Properties然后选择选项卡Java Build Path和第一个子选项卡Sources There you can see the currently registered source folders and you can press Add Folder to register some more folders (from your project's main folder) as source folders. 在这里您可以看到当前注册的源文件夹,并且可以按Add Folder将更多文件夹(从项目的主文件夹中)注册为源文件夹。

However, keep in mind that it's generally easier and a best practice to have only 1 source folder, which is src and to put all your code inside it, organized by packages. 但是,请记住,通常只有一个源文件夹(即src并将所有代码放入其中(按包组织),这更容易,也是一种最佳做法。

Package folder should be inside the same directory as the java file importing it. Package文件夹应与导入它的Java文件位于同一目录内。

so if the main.java importing the snake package they should have the same path 因此,如果main.java导入蛇包,它们应该具有相同的路径

src/snake src /蛇

src/game.java src / game.java

If they are not in the same folder than better keep them in the same folder(If it is fine with your program) cause it's easier that way. 如果它们不在同一文件夹中,则最好将它们保留在同一文件夹中(如果您的程序可以使用),这样会更容易。

src文件夹下创建一个名为“ snake”的文件夹,然后将您的移动文件放在此处

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

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