问题描述
假设我们有一个名为 com.example1
的包,其中包含 Hello
类(以及其他类)。
Suppose we have a package called com.example1
containing a Hello
class (along with other classes).
然后我们有另一个包 com.example2
还包含 Hello
class(显然有不同的行为)。
Then we have another package com.example2
also containing a Hello
class (obviously with different behaviour).
现在让我们假设我们需要com.example1中的每个类和com.example2中的Hello类
Now let's suppose we need every class in com.example1 and the Hello class in com.example2
import com.example1.*;
import com.example2.Hello;
在这种情况下会调用哪一个?
Which one gets called in this case?
Hello hello = new Hello();
或者这是否会产生编译错误?
Or does this give a compile error?
这只是一个出于好奇的理论问题。
This is just a theoretical question out of curiosity.
由于创建包是为了避免命名冲突,当两个包包含两个具有相同名称的类时会发生什么? / p>
Since packages were created to avoid naming conflict, what happens when two packages contain two classes with the same name?
推荐答案
它会产生编译错误。你必须明确地命名这个类 - com.example2.Hello hello = new com.example2.Hello();
It will give a compile error. You have to explicitly name the class - com.example2.Hello hello = new com.example2.Hello();
这篇关于解决包冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!