我在 JLS 7 中读到了以下句子。
It is a compile-time error to use the name of a type parameter of any surrounding declaration in the header or body of a class method.
请解释其含义。
最佳答案
这意味着你不能这样做
class Test<T> {
static void f(T a) {
}
}
你必须做
class Test<T> {
static <T> void f(T a) {
}
}
静态方法的泛型类型独立于类或其他方法的泛型类型。
所以你甚至可以做类似的事情
class Test {
static <A, B, C> void f(A a, B b, C c) {
}
}
关于java - Java 语言规范中的静态方法和类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23032381/