包的导入会改变类的可见性吗?
我刚刚了解到
一个类可以用 修饰符 public,在这种情况下 类对所有类可见 到处。如果一个类没有修饰符 (默认值,也称为 包私有),仅可见 在它自己的包中。
这是一个明确的声明。但这些信息干扰了我对包导入的理解(这很容易出错)。我认为导入包会使导入包中的类对导入类可见。
那么,它是如何运作的呢? 在导入包含公共类的包的情况下,公共类是否对任何地方的所有类都可见?或者说没有这个条件?包私有类怎么样?无论包含的包是否导入,它们都是不可见的?
额外: 在我看来,我得到了 2 个答案,它们被标记为好(已投票)并且彼此矛盾。
I jsut learned that
A class may be declared with the
modifier public, in which case that
class is visible to all classes
everywhere. If a class has no modifier
(the default, also known as
package-private), it is visible only
within its own package.
This is a clear statement. But this information interfere with my understanding of importing of packages (which easily can be wrong). I thought that importing a package I make classes from the imported package visible to the importing class.
So, how does it work? Are public classes visible to all classes everywhere under condition that the package containing the public class is imported? Or there is not such a condition? What about the package-private classes? They are invisible no mater if the containing package was imported or not?
ADDED:
It seems to me that I got 2 answers which are marked as good (up-voted) and which contradict eachother.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
导入类不会以任何方式改变其可见性。将一个类导入到另一个类或多或少只是一种使源代码可读的方法,因此您不必始终放入完全限定的类。例如,此类
编译为与此类相同的代码。
第一个类中的
import
语句不会更改Collection
或中的任何其他类的可见性。 >java.util
包它只是使ImportTests
类可以引用Collection
而无需完全限定名称。Importing a class doesn't change its visibility in any way. Importing a class to another class is more or less just a way to make your source code readable so you don't have to put in fully qualified class all the time. For example this class
compiles to the same code that this class would
The
import
statement in the first class doesn't change the visibility ofCollection
or any other class inside thejava.util
package it just makes it so theImportTests
class can referenceCollection
without the fully qualified name.您不需要导入路径或类来使其可见。
要使类或路径可见,您必须在编译或执行期间在类路径声明中指定。
“import”指令(C# 中的“using”指令)只是让我们变得懒惰。
如果您有课程,
您可以随时引用它们的完整路径,而无需导入它们:
但是,懒惰是创造力的美德,我宁愿做
哪个,您可能已经意识到了。
1 - 那么问题是,
如果有两个同名但命名空间不同的类,编译器将使用哪个类?
编译器会发出错误消息 - 日期类冲突
并且你将无法成功编译。
因此,您必须导入其中一个并使用另一个及其完整路径。
在 C# 中,我们可以导入
,这样我们就可以这样做,
但是,要么一如既往,java 独裁者要么害羞/自豪地允许自己允许 java 模仿微软,要么微软拥有这种形式的导入专利。
2 - 第二个问题是,
如果我们有一个类,
那么你做了一个导入
,当你声明
将使用哪个字符串时? java.lang.String 还是 atlantic.salmon.are.trouts.String?
编译器将选择并遵守 import 语句并使用 atlantic.salmon.are.troouts.String。
3 - 第三个问题,
私有、受保护、公共可见性修饰符和默认可见性根本不要与导入指令混淆。除了使用相同的语言之外,别无他法。
在同一个文件内。
在相同的命名空间包中或
通过扩展类。
仅在相同的范围内可见
命名空间包。
导入指令根本不会改变这些行为。
总之,
美德的延续
懒惰。
使类可见的目的或
改变他们的可见性
内容。
整个项目可见的类。
Java 中的可见性修饰符
汇编。
You do not need to import a path or a class to make it visible.
To make classes or paths visible, you have to specify at classpath declaration during compilation or execution.
"import" directive ("using" directive in C#) merely helps us to be lazy.
If you have classes
you could always refer them with their full paths without importing them:
However, laziness being the virtue of creativity, I would rather do
Which, you probably have already realised.
1 - The question would then be,
if there are two classes of the same name but different namespace, which would be used by the compiler?
The compiler would complain with error message - conflicting classes for Date
and you would not be able to compile successfully.
So you have to import one of them and use the other with its full path.
In C# we could import
So that we could do,
However, either as always, the java authoritarians are either to shy/proud to allow themselves to allow java immitate Microsoft or that Microsoft has a patent on such form of import.
2 - The second question would be,
if we had a class
Then you did an import
And when you declare
which String would be used? java.lang.String or atlantic.salmon.are.trouts.String?
The compiler would pick and obey the import statement and use atlantic.salmon.are.trouts.String.
3 - the third issue,
private, protected, public visibility modifiers and default visibility are not to be confused with the import directive at all. Nothing to do except being in the same language.
within the same file.
within the same namespace packages or
by an extension class.
are visible only within the same
namespace packages.
Import directive does not change these behaviours at all.
In conclusion,
the continuance of the virtue of
laziness.
purpose of making classes visible or
changing the visibility of their
contents.
classes visible to the whole project.
of visibility modifiers in a Java
compilation.
我有两个包A和C。A中有一个名为Random的类。我这里的代码编译得很好,随机数来自 A 而不是来自 java.util。 java.util 导入在 Eclipse 中向我发出警告,提示导入未使用。
这是隐藏类的另一个例子。
这是我的随机课程。
当我运行 C main 方法时,我得到......
I have two packages A and C. A has a class named Random in it. My code here compiles fine, and the random is from A and not from java.util. The java.util import gives me a warning in eclipse that the import is unused.
Here is another example of the hiding of classes.
Here is my Random class.
When I run the C main method I get...
如果您想使用
B 类
中的A 类
,并且B 类
不位于同一个包与类A
,您必须导入类B
,即使类B
是<代码>公共。如果不需要导入公共类,就无法声明两个同名的类。
If you want to use
class A
fromclass B
, andclass B
is not in the same package withclass A
, you must importclass B
, even if theclass B
ispublic
.If public classes didn't need to be imported, there could be no way of declaring two classes with the same name.