标签:泛型
关于泛型限定:
<? extends E >向上限定。意思就是说只能传递E类型以及E类型的子类;
代码:
package itheima; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * 泛型限定 * @author Administrator * */ public class GenericityLimit { public static void main(String[] args) { ArrayList<Student> students=new ArrayList<Student>(); students.add(new Student("student1")); students.add(new Student("student2")); students.add(new Student("student3")); printColl(students); } //定义一个迭代方法 public static void printColl(ArrayList<? extends Person> list){ Iterator<? extends Person> it=list.iterator(); while(it.hasNext()){ System.out.println(it.next().getName()); } } } class Person{ private String name; protected Person(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class Student extends Person{ public Student(String name){ super(name); } }运行结果:
标签:泛型
原文地址:http://blog.csdn.net/u014010769/article/details/46137375