我正在使用带有包含函数的泛型类TestThrows<T>
,该函数返回一个通用列表。我的问题是我无法编译这个程序,它抛出以下错误:通用列表和for-each循环
类型不匹配:不能从元素类型的对象转换为Throwable的
public class Test
{
public static void main(String[] args)
{
TestThrows testThrows = new TestThrows();
// compile error on the next line
for (Throwable t : testThrows.getExceptions())
{
t.toString();
}
}
static class TestThrows<T>
{
public List<Throwable> getExceptions()
{
List<Throwable> exceptions = new ArrayList<Throwable>();
return exceptions;
}
}
}
我不知道这是为什么错误的我正在使用通用列表?
您使用TestThrows的原始类型。这扰乱了你的方法调用的泛型。 –