简体   繁体   English

从空包导入所有内容

[英]Importing everything from an empty package

I have a package named utilities which has several sub-packages. 我有一个名为Utility的程序包,其中包含几个子程序包。 The utilities package has no classes by itself but the sub-packages do. 该实用程序包本身没有类,但子包却有。

I can import all the classes from the sub-packages one by one by doing this: 通过执行以下操作,我可以从子包中一个接一个地导入所有类:

import utilities.consoleredirect.MessageConsole;
import utilities.generalutils.Helper;
import utilities.generalutils.Pair;
import utilities.generalutils.PropertiesUtils;
import utilities.sqlhandling.Connector;
import utilities.sqlhandling.SQLDatabase;
import utilities.sqlhandling.User;
import utilities.tweetshandling.TwitterTools;
import utilities.tweetshandling.WordCounting;

But if I try to do import utilities.*; 但是,如果我尝试import utilities.*; I get the error that the package does not exist (in my IDE - NetBeans) but if I try to create the package then I get the error that the file already exists. 我收到该软件包不存在的错误(在我的IDE-NetBeans中),但是如果尝试创建该软件包,则会收到该文件已经存在的错误。 The folder structure is correct, this is it for easier visualization: 文件夹结构正确,是为了方便可视化:

1

So why can't I just do import utilities.*; 那么为什么我不能仅仅import utilities.*; and I have to manually import every class? 并且我必须手动导入每个类?

You can't use a wildcard in an import statement to import other packages , it's just for classes. 您不能在import语句中使用通配符来导入其他 ,仅用于类。

So you could do: 因此,您可以执行以下操作:

import utilities.consoleredirect.*;
import utilities.generalutils.*;
import utilities.sqlhandling.*;
import utilities.tweetshandling.*;

If you're using a sensible IDE, you won't need to think about this very much. 如果您使用的是明智的IDE,则无需考虑太多。 Just try and use a class by name and the import statement will be added automagically. 只需尝试使用名称命名的类,即可自动添加import语句。

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

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