java 如何在一个java文件中导入多个包?

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

how to import multiple packages in one java file?

javapackages

提问by Akshay Soma

How can I import multiple packages in one java file. For example if I created a main method in one java file and packages in different. I want to import 3 packages in one line then how can I import all in one line?

如何在一个 java 文件中导入多个包。例如,如果我在一个 java 文件和不同的包中创建了一个 main 方法。我想在一行中导入 3 个包,那么如何在一行中导入所有包?

回答by VAIBHAV B.V.S

In Java, you can import entire package using *. to import multiple packages. e.g. import com.package1.*;to import everything from package1. To import multiple packages, e.g.import com.package1.*; import com.package2.*; import com.package3.*;

在 Java 中,您可以使用*. 导入多个包。例如import com.package1.*;从 package1 导入所有内容。导入多个包,例如import com.package1.*; import com.package2.*; import com.package3.*;

Importing on a single line like import package1.*,package2.*,package3.*;is not possible in java but you can import entire directory if the packages are in the same directory as in import com.*;

import package1.*,package2.*,package3.*;在 java 中不可能像这样在单行上导入,但是如果包与在同一目录中,则可以导入整个目录import com.*;

回答by Nate Vaughan

The wildcard *can import everything from a single package:

通配符*可以从单个包中导入所有内容:

import junit.*;

Just be sure you don't clutter your local namespacewith classes you are not using. If you're using a Java IDE, consider using the Organize Imports feature (ctrl-shift-o in Eclipse) instead.

请确保不要将本地命名空间与未使用的类混淆。如果您使用的是 Java IDE,请考虑改用“组织导入”功能(Eclipse 中的 ctrl-shift-o)。