java - Generic method declaration from examlab -


abstract class typetest<k extends number> {     public abstract <k> k useme(object k); //ok     public abstract <k> typetest<? extends number> useme(typetest<? super k> k);  //ok     public abstract <k> typetest<? super number> useme(typetest<? extends k> k);  //error     public abstract <v extends k> typetest<v> useme(typetest<v> k);  //ok } 

answer examlab declaration above ok,but found 3rd compiled error.and want know why,thanks!

i found fine in eclipse compile error in editplus jdk1.7,i want confirm whether 3rd line declaration right.

compile error info

typetest.java:4: 错误: 类型参数? extends k#1不在类型变量k#2的范围内     public abstract <k> typetest<? super number> useme(typetest<? extends k>  k);  //error                                                                 ^   其中, k#1,k#2是类型变量:     k#1扩展已在方法 <k#1>useme(typetest<? extends k#1>)中声明的object     k#2扩展已在类 typetest中声明的number 1 个错误 请按任意键继续. . . 

i tried times change compile info chinese english fail messy code"??". thanks!

this answer reference forums:

note method type parameter not same class type parameter . method type parameter shadows class type parameter .

in method

view plainprint? note: text content in code blocks automatically word-wrapped public abstract <k> typetest<? super number> useme(typetest<? extends k> k);   

the type parameter resolved java.lang.object upper bound resolved.

but typetest include cases ( example typetest ) not within higher bound set class parameter cause per class parameter typetest's type parameter can extends number.

hence compilation fails.

note compiles jdk versions 1.6.0_43 or lower fails jdk versions 1.6.0_45 or higher.

i guess question trying test if understand method type parameter shadows class type parameter.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -