如何绑定只能静态导入的方法的类型变量? [英] How to bind the type variable of a method that can only be statically imported?
问题描述
我有两个名为 Matchers
的类,我需要在第三个类中使用静态方法,因此其中一个只能通过静态导入使用。但是,我无法为静态导入的方法明确指定类型参数(即< Type> method()
)。
你不能。至少不是在Java中(其他一些语言,例如Scala,可以给你导入一个类的别名,这种类很酷,可以在这种情况下使用)。
在这里,您只需要为两个类或一个类使用全限定名(并导入另一个类)。
使用2个类除非你有一个非常好的用例,否则一个文件中的同名不是最好的想法。也许你只用一个Matchers就可以做你想做的事情?
我会在这里做一些头脑的阅读,但我想这是沿着Hamcrest / Mockito Matchers线路?如果是这样,那么在大多数情况下,您只需使用其中一个即可。如果您遇到非常不幸的情况,那么您做需要两种方法,那么,您是不幸的: - )
I have two classes called Matchers
with static methods that I need to use in a third class, so one of them can only be used via static imports. However, I cannot explicitly specify type parameters (i.e. <Type> method()
) for statically imported methods.
How can I work around this without having to fully qualify the class?
You cannot. At least not in Java (some other languages, for instance Scala, let you give an alias to a class you import which is kind of cool and can be used exactly in such a case).
Here you just need to either use the fully qualified name for both classes or for one class (and import the other one).
Using 2 classes with the same name in one file isn't the best idea unless you have a really good use case for it. Maybe you can actually do what you want using only one of the Matchers?
I'm going to do some mind reading here but I'd guess this is something along the lines of Hamcrest/Mockito Matchers? If so then in most cases you should be ok with using only one of them. If you're in the very unfortunate case where you do need both then, well, you're unfortunate :-)
这篇关于如何绑定只能静态导入的方法的类型变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!