日期:2014-05-20 浏览次数:20926 次
public void genericTest(List<ClassA> testList){//单个ClassA。ClassB逻辑完全和这个一样
for(ClassA cA : testList){
if(cA.getAge() > 60){
//......
}
}
}
public void genericTest(List<?> testList){//不能生效的泛型应用
for(Object obj : testList){
if(obj.getAge() > 60){
//......
}
}
}
public static void main(String[] args) {
List target = new ArrayList();
target.add(new ClassA());
target.add(new ClassB());
Test t = new Test();
t.genericTest(target);
}
public void genericTest(List<?> testList){
try{
for(Object obj : testList){
Method m = obj.getClass().getDeclaredMethod("getAge");
if(Integer.valueOf(m.invoke(obj).toString()) > 60){
System.out.println(obj.getClass().getName()+" gt");
}
}
}catch(Exception e){
e.printStackTrace();
}
}