我有以下 Java 程序,我原以为它不会编译,但它编译了:
class Test {
public static void f() {
}
void m() {
Test.<String>f();
}
}
为什么javac允许以这种方式调用非参数化方法?
我的Java编译器版本是:javac 1.7.0_75
最佳答案
显式类型参数会被忽略。
这在 JLS, Section 15.12.2.1 中有说明:
- If the method invocation includes explicit type arguments, and the member is a generic method, then the number of type arguments is equal to the number of type parameters of the method.
This clause implies that a non-generic method may be potentially applicable to an invocation that supplies explicit type arguments. Indeed, it may turn out to be applicable. In such a case, the type arguments will simply be ignored.
关于java - 为什么使用类型参数调用非参数化方法不会出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28584336/