public class FatherA { private String a; public String getA() { return a; } public void setA(String a) { this.a = a; } }
子类
1 2 3 4 5 6 7 8 9 10 11 12 13
public class Children extends FatherA { private String b; public String getB() { return b; } public void setB(String b) { this.b = b; } }
实现
List<FatherA> 和 List<Children> 是两种不同的泛型类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class Test { public static void main(String[] args) { Children cl = new Children(); cl.setB("1"); cl.setA("2"); List<Children> list = new ArrayList<Children>(); list.add(cl); getA(list); System.out.println(cl.getA()); } private static <T extends FatherA> void getA(List<T> list) { for (int i = 0; i < list.size(); i++) { list.get(i).setA("3"); } } }