如何在不同的包中导入两个具有相同名称的类? [英] How to import two classes with the same name in different packages?
问题描述
我想导入这两个类,名为 Query
- 一个是 JDO 类,另一个是 JPA class,在同一个类中使用不同的方法。
import javax.jdo.Query;
import javax.persistence.Query;
有没有办法在文件顶部同时全局导入它们?
我害怕,不。但是您不必导入类来使用它:只需按其全名引用其中一个类,例如
javax .jdo.Query query = getJDOQuery();
query.doSomething();
然后你可以导入另一个没有名字冲突。
顺便说一句,有时候如果你开始在你的班级中得到很多这样的名字这样的碰撞,那就是重构的一个微妙的暗示:将一个大类的功能分成几个小类。
I want to import these two classes, both named Query
- one a JDO class, the other a JPA class, to use in different methods in the same class.
import javax.jdo.Query;
import javax.persistence.Query;
Is there a way to globally import both of them at the same time at the top of the file?
I'm afraid, no. But you don't have to import class to use it: just reference one of the classes by its full name, like
javax.jdo.Query query = getJDOQuery();
query.doSomething();
Then you can import another without name collisions.
BTW, sometimes if you start getting lots of such name such collisions in your class, it's a subtle hint for refactoring: splitting functionality of one big class between several small ones.
这篇关于如何在不同的包中导入两个具有相同名称的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!