Java 不同包中的相同类名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4055634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Same class name in different packages
提问by Rachel
Can same class exist in multiple packages?
In other words, can I have Foo.java
class in com.test.package1
and com.test.package2
?
同一个类可以存在于多个包中吗?换句话说,我可以有Foo.java
类com.test.package1
和com.test.package2
?
Update
更新
Now I copied class from package 1 and placed in to package 2 and now I am creating an instance of that class, I want this instance to point to class present in package 1 but currently it points to package1 path, how can i modify it?
现在我从包 1 复制了类并放入包 2 中,现在我正在创建该类的一个实例,我希望这个实例指向包 1 中存在的类,但目前它指向包 1 路径,我该如何修改它?
Oh so I cannot do something like:
哦,所以我不能做这样的事情:
Foo = new Foo() // pointing to Foo class in package 1
Foo = new Foo() // pointing to Foo class in package 2
采纳答案by aioobe
Yes, you can have two classes with the same name in multiple packages. However, you can't import both classes in the same fileusing two import
statements. You'll have to fully qualify one of the class names if you really need to reference both of them.
是的,您可以在多个包中拥有两个同名的类。但是,您不能使用两个import
语句在同一个文件中导入两个类。如果您确实需要引用它们两个,则必须完全限定其中一个类名。
Example: Suppose you have
例子:假设你有
pkg1/SomeClass.java
pkg1/SomeClass.java
package pkg1;
public class SomeClass {
}
pkg2/SomeClass.java
pkg2/SomeClass.java
package pkg2;
public class SomeClass {
}
and Main.java
和Main.java
import pkg1.SomeClass; // This will...
import pkg2.SomeClass; // ...fail
public class Main {
public static void main(String args[]) {
new SomeClass();
}
}
If you try to compile, you'll get:
如果你尝试编译,你会得到:
$ javac Main.java
Main.java:2: pkg1.SomeClass is already defined in a single-type import
import pkg2.SomeClass;
^
1 error
This howeverdoes compile:
然而,这确实编译:
import pkg1.SomeClass;
public class Main {
public static void main(String args[]) {
new SomeClass();
new pkg2.SomeClass(); // <-- not imported.
}
}
回答by wheaties
Sure can but you'll need to distinguish which one you want when calling them in other packages if both are included within a source file.
当然可以,但如果两者都包含在源文件中,则在其他包中调用它们时,您需要区分您想要哪个。
Response to Comment:
回复评论:
com.test.package1.Foo myFoo = new com.test.package1.Foo();
com.test.package2.Foo myOtherFoo = new com.test.package2.Foo();
回答by Percy
i was taken to this page by google when i had the error a type with the same simple name is already defined by the single-type-import
. i fixed this error (AFTER A VERY LONG TIME) by realising the line import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
had snuck into the very top of my imports whilst i had the line import org.apache.commons.codec.binary.Base64;
at the bottom of my imports.
当我遇到错误时,我被谷歌带到了这个页面a type with the same simple name is already defined by the single-type-import
。我修复了这个错误(在很长一段时间之后),因为我意识到这条线import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
已经潜入了我的进口的最顶部,而我的进口线import org.apache.commons.codec.binary.Base64;
在我的底部。
回答by Jakob Holst
So I was looking for a smarter solution than just using fully qualified names on one or both of the implemented classes.
因此,我正在寻找一种比仅在一个或两个实现的类上使用完全限定名称更智能的解决方案。
If you create a private class, and extend your class, you are free to use the class, without writing the full package name each time.
如果您创建一个私有类,并扩展您的类,您可以自由使用该类,而无需每次都写出完整的包名。
Package 1
套餐一
namespace namespace1.Logger
{
public class Log
{
public void Foo1(){}
}
}
Package 2
套餐二
namespace namespace2.Logger
{
public class Log
{
public void Foo2(){}
}
}
My class implementation
我的类实现
//using namespace1.Logger;
//using namespace2.Logger;
namespace MyProject
{
public class MyClass
{
public MyClass()
{
LoggerA a = new LoggerA();
LoggerB b = new LoggerB();
a.Foo1();
b.Foo2();
}
private class LoggerA : namespace1.Logger.Log { }
private class LoggerB : namespace2.Logger.Log { }
}
}