简体   繁体   English

如何从Eclipse导入包?

[英]How to import a package from Eclipse?

In one of my directories I have all .java files which belong to one package ("game"). 在我的一个目录中,我拥有属于一个包(“游戏”)的所有.java文件。 Now I want to create one .java file which does not belong to this package and which imports the "game" package. 现在我想创建一个不属于这个包并导入“游戏”包的.java文件。 If I create a new file and write import game; 如果我创建一个新文件并编写import game; then Eclipse complains that it does not know what the "game" package means. 然后Eclipse抱怨它不知道“游戏”包的含义。 Can somebody please help me to solve this problem? 有人可以帮我解决这个问题吗?

You cannot import a package, you need to import classes from that package: 您无法导入包,您需要从该包导入类:

import game.SomeClass;
import game.SomeOtherClass;

or 要么

import game.*;

to import all classes from that package. 从该包导入所有类。

Eclipse can generate these imports for you if you type the class-names in your code and press Ctrl + Space for Code-Completion. 如果在代码中键入类名并按Ctrl + Space进行代码完成,Eclipse可以为您生成这些导入。 Select the correct class there (if more classes with the same name exist in different packages), and the import will be generated. 在那里选择正确的类(如果不同的包中存在更多具有相同名称的类),则将生成导入。

You might also want to use Eclipse's 'Organize imports' shortcut: Ctrl + Shift + O. 您可能还想使用Eclipse的“组织导入”快捷方式:Ctrl + Shift + O.

This will find all missing types and add the appropriate imports automatically. 这将找到所有缺失的类型并自动添加适当的导入。

在您的新java文件中导入您的包或创建对象,并从您的旧包中获取您想要的java类

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

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