用显式类型参数调用静态导入的方法 [英] Invoking statically imported method with explicit type parameters
本文介绍了用显式类型参数调用静态导入的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里是我的问题的后续处理:奇怪的Java通用。
如果我有这样的代码:
Casts。< X,T>铸(可迭代[指数]);
我可以添加一个静态导入并执行操作:
< X,T>铸(可迭代[指数]);
Eclipse不允许这样做。但是,在Eclipse中看到如此多的静态导入错误后,我并不确定。 解决方案
不,你不能:我通过一些测试代码证实了这一点。
PS> javac -version
javac 1.6.0_04
Casts.java
public class Casts
{
public static< From,To>转换(最终来自对象)
{
return(To)object;
}
}
Test.java
import static Casts.cast;
$ b $ public class Test {
{
public static void main(String [] args)
{
final Integer integer = new Integer(5);
//这个编译好。
final数字数字=转换。<整数,数字> cast(整数);
//这个编译失败:
// PS> javac Test.java
// Test.java:11:表达式的非法开始
//最终数字编号=<整数,数字> cast(integer);
// ^
// Test.java:11:不是语句
//最终数字数字=< Integer,Number> cast(integer);
// ^
final String string =< Integer,String> cast(integer);
}
}
This is the follow up of my question here: Weird Java generic.
If I have a code like this:
Casts.<X, T> cast(iterable[index]);
Can I add a static import and do:
<X, T> cast(iterable[index]);
Eclipse doesn't allow this. But after seeing so many bugs with static import in Eclipse, I'm not that certain.
解决方案
No, you can't : I just confirmed this via some test code.
PS > javac -version
javac 1.6.0_04
Casts.java
public class Casts
{
public static <From, To> To cast(final From object)
{
return (To)object;
}
}
Test.java
import static Casts.cast;
public class Test
{
public static void main(String[] args)
{
final Integer integer = new Integer(5);
// This one compiles fine.
final Number number = Casts.<Integer, Number>cast(integer);
// This one fails compilation:
// PS> javac Test.java
// Test.java:11: illegal start of expression
// final Number number = <Integer, Number>cast(integer);
// ^
// Test.java:11: not a statement
// final Number number = <Integer, Number>cast(integer);
// ^
final String string = <Integer, String>cast(integer);
}
}
这篇关于用显式类型参数调用静态导入的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文