使用Eclipse4.2和Java 7,并尝试实现下面的列表接口方法,我得到了一个警告。
public <T> T[] toArray(T[] a) {
return a;
}警告说:
类型参数T隐藏类型T

为什么?我怎么才能摆脱它?
发布于 2012-09-22 22:30:43
列表接口也是通用的。确保您没有在类中对泛型类型使用T。注意,在http://docs.oracle.com/javase/6/docs/api/java/util/List.html中,类泛型参数使用"E“,toArray()泛型参数使用"T”。这样可以防止重叠。
public class MyList<T> implements List<T> {
// V1 (compiler warning)
public <T> T[] toArray(T[] array) {
// in this method T refers to the generic parameter of the generic method
// rather than to the generic parameter of the class. Thus we get a warning.
T variable = null; // refers to the element type of the array, which may not be the element type of MyList
}
// V2 (no warning)
public <T2> T2[] toArray(T2[] array) {
T variable = null; // refers to the element type of MyList
T2 variable2 = null; // refers to the element type of the array
}}
发布于 2015-10-22 21:51:58
另一个选项是,您有一个名为"T“的类的导入,这就是为什么要收到警告的原因。我刚刚解决了我的问题后,我发现我有一个无用的导入:
org.apache.poi.ss.formula.functions.T医生:检查你的进口品!
https://stackoverflow.com/questions/12548205
复制相似问题