不应导入import foo.*& quot;还包括子包& quot; foo.bar.*& quot;吗? [英] Shouldn't "import foo.*" also include subpackage "foo.bar.*"?
问题描述
在学习Java时,我想到了许多教程的一个令人困惑的属性.考虑一下示例教程中的以下两个导入:
Studying Java, I've thought about a, to me, rather confusing property of many tutorials. Consider the following two imports from a sample tutorial:
import java.awt.*;
import java.awt.event.*;
第一行显然是导入java.awt包,第二行是awt的子包.但是星号不应该包含所有子包吗?因此,第一行应该可以解决这个问题-不需要第二行吗?如果不是:星号的真正用途/用途是什么?
The first line obviously imports the java.awt package, and the second one awt's subpackage. But shouldn't the asterix include all sub-packages? Hence, line one should do the trick - line two shouldn't be needed? If it doesn't: then what's the true purpose/usage of the asterix?
例如,在MySQL中使用 SELECT * FROM foo
从表中选择所有字段,也许我很愚蠢地认为这种情况自然而然.
For example, using SELECT * FROM foo
in MySQL selects ALL fields from a table, perhaps I'm stupid to assume that this naturally is t he case.
推荐答案
不,软件包是作为一个整体来考虑的.尽管从层次上考虑它们通常是有用的,但Java语言或编译器中没有任何表示 java.awt.event
属于 java.awt的概念
.
No, packages are taken as a whole. Even though it's often useful to think of them hierarchically, there is no notion within the Java language or compilation that says java.awt.event
belongs to java.awt
.
您与SQL表的比较不太正确,因为在SQL数据库中没有子表之类的东西.相反,假设您有一个代表所有类的表,其中包含以下条目:
Your comparison with SQL tables isn't quite right because there's no such thing as a sub-table in SQL databases. Instead, imagine you had a table representing all your classes, with the following entries:
ID | Package | Name
--------------------------
1 | awt | SomeClassName1
2 | awt.event | SomeClassName2
现在,如果您想获得awt类,您会说:
Now, if you wanted to get awt classes, you'd say:
SELECT * FROM MyTable WHERE Package = 'awt'
您不会期望这会给您两个条目,只是因为软件包名称以 awt
开头,对吗?
You wouldn't expect this to give you both entries, just because the package name starts with awt
, would you?
这篇关于不应导入import foo.*& quot;还包括子包& quot; foo.bar.*& quot;吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!