为什么拳头调用f()编译好?给出的参数(和Boxed)是Integer,但显式类型见证是String!它应该在编译时失败 - 就像它在第二次调用中失败一样。
class Test<T> {
<S> void f(S arg) {
}
}
// for My<T> both f() calls give same resuls...
public class My {
public static void main(String[] args) {
Test obj = new Test<>();
obj.<String>f(4); // compiles (and runs) fine!
//c.ERR
//parameterized method <String>f(String) of type Test<Object>
//is not applicable for the arguments (Integer)
new Test<>().<String>f(4);
但是如果我使Test不是泛型类,则编译会按预期失败:
class Test {
<S> void f(S arg) {
}
}
public class My {
public static void main(String[] args) {
Test obj = new Test();
// compile err
//parameterized method <String>f(String)
//of type Test is not applicable for the arguments (Integer)
obj.<String>f(4);
}
}
P.S。 Eclipse编译器,SE7模式