不应该“import foo。*”还包括子包“foo.bar。*”? [英] Shouldn't "import foo.*" also include subpackage "foo.bar.*"?

查看:137
本文介绍了不应该“import foo。*”还包括子包“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的子包。 但是asterix不应该包含所有子包吗?因此,第一行应该做的诀窍 - 不应该需要第二行?如果没有:那么星号的真正用途/用途是什么?

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?

例如,使用 SELECT MySQL中的FROM 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表的比较是不对的,因为没有sub这样的东西-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。*”还包括子包“foo.bar。*”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆