本帖最后由 伍叶竹 于 2014-4-17 13:39 编辑
- import java.util.*;
- class Text02
- {
- public static void main(String[] args)
- {
- ArrayList<Person> al = new ArrayList<Person>();
- al.add(new Person("abc1"));
- al.add(new Person("abc2"));
- al.add(new Person("abc3"));
- printColl(al);
- ArrayList<Student> al1 = new ArrayList<Student>();
- al1.add(new Student("abc--1"));
- al1.add(new Student("abc--2"));
- al1.add(new Student("abc--3"));
- printColl(al1);
- }
- public static void printColl(Collection<? extends Person> al)
- {
- Iterator<? extends Person> it = al.iterator();
- while(it.hasNext())
- {
- System.out.println(it.next().getName());
- }
- }
-
- }
- class Person
- {
- private String name;
- Person(String name)
- {
- this.name = name;
- }
- public String getName()
- {
- return name;
- }
- }
- class Student extends Person
- {
- Student(String name)
- {
- super(name);
- }
- }
复制代码 <? extends Person>用<? super Student>代替的话,如何修改程序?
还是泛型限定 。 昨晚断网后纠结的问题。
|