我的目标是实现一个接受类实例的操作,但前提是具体传递类继承自Collection。如何使用泛型正确指定类<?>参数
我在我的代码上得到编译器警告,我无法解决。
package com.fun.with.generics;
import java.util.ArrayList;
import java.util.Collection;
public class AcceptTypedClass
{
// Warning here on 'Collection': ... raw type, references should be parameterized.
private static void printCollectionClass(Class<? extends Collection> c)
{
System.out.println(c.getName());
}
public static void main(String[] args)
{
// Warning here on ArrayList: ... raw type, references should be parameterized.
Class<ArrayList> arrayList = ArrayList.class;
printCollectionClass(arrayList);
}
}
我明白的警告信息,但如果我通过添加缺少的类型参数如下图所示修复警告,我得到一个错误。
Class<ArrayList<?>> arrayList = ArrayList.class;
有人可以解释一下吗?问题是如果我可以编写上面的代码,以便我不会发出警告(并且代码有效)?
你有没有试过Class?扩展集合>>'?请注意,进一步向下传递'ArrayList',其中'Class'是预期的。 –