public class Test {
public <T extends List> T getConnection(String className){
T obj = null;
try{
Class classobj = Class.forName(className);
obj = (T)classobj.getConstructor(new Class[]{int.class}).newInstance(1);
}catch(Exception e){
e.printStackTrace();
}
return obj;
}
public static void main(String args[]){
Teste t = new Teste();
List list = t.getConnection("java.util.ArrayList");
list.clear();
}
}
方法上使用泛型
最新推荐文章于 2022-07-21 10:24:17 发布