Java - 无法导入com.package。*(通配符) [英] Java - Can't import com.package.*(wildcard)
问题描述
这可能是一个简单的问题,但Google没有返回任何内容。
This may be a simple issue, but Google has returned nothing.
I仍然不明白为什么直接导入会起作用但是通配符不会。
通过类包我的意思是一个类包。我还是Java新手所以我不知道语义
I still don't understand why a direct import will work but wildcard will not. By class package I mean a package of classes. I am still new to Java so I do not know the semantics
我有一个类包: com.company .functions
,当我尝试 import com.company。*
我收到以下错误。
I have a class package: com.company.functions
, when I try to import com.company.*
I receive the following error.
java: package com.company does not exist
如果我明确导入 com.company.function
,那么我没有任何问题。
If I import com.company.function
explicitly then I have no issues.
所以我想我有一个解决方案,但为了学习,有人可以解释为什么我会看到这个问题吗?
So I suppose that I have a solution but for the sake of learning, could someone explain why I see this issue?
IDE:IntelliJ IDEA 12
IDE:IntelliJ IDEA 12
import com.sociapathy.*; <--Throws compile error java: package com.sociapathy does not exist
import com.sociapathy.databaseAccess.MySQL; <--Works just fine
推荐答案
听起来像你'尝试导入一个不包含类但只包含子包的包。
It sounds like you're trying to import a package that contains no classes, but only contains sub-packages.
ie您有 com.company.functions
- 中的课程,例如 com.company.functions.Foo
但是没有类直接在 com.company
- 例如 com.company.Bar
i.e. You have classes in com.company.functions
- e.g. com.company.functions.Foo
But no classes directly in com.company
- e.g. com.company.Bar
尽管java软件包似乎是分层的,但为了导入的目的,它们不是。
Despite that fact that java packages appear to be hierarchical, for the purposes of imports, they aren't.
因此,您无法导入 com.company。*
,因为它不包含自己的类。
您可以导入 com.company.functions。*
因为它确实包含类
您可以导入 com.company.functions.Foo
因为这是一个类。
So, you cannot import com.company.*
because it contains no classes of its own.
You can import com.company.functions.*
because it does contain classes
And you can import com.company.functions.Foo
because that is a class.
现在,你可能会想要创建一个 Bar
类在 com.company
中。
这将允许您导入 com.company。*
但因为不会对层次结构进行处理,这不会导致 com.company.functions
中的类被导入
你仍然需要明确地导入 com。 company.functions.Foo
,或通配符导入 com.company.functions。*
Now, you might be tempted create a Bar
class in com.company
.
That would allow you to import com.company.*
But because imports are not treated hierarchically, that wouldn't cause classes in com.company.functions
to be imported
You would still need to explictly import com.company.functions.Foo
, or wildcard import com.company.functions.*
这篇关于Java - 无法导入com.package。*(通配符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!