下面代码如何改成下限方法,我尝试修改了,编译出错。求解答
- import java.util.*;
- 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);
- }
- }
- class TreeSetDemo9
- {
- public static void main(String[] args)
- {
- ArrayList<Person> al = new ArrayList<Person>();
- al.add(new Person("java01"));
- al.add(new Person("java02"));
- al.add(new Person("java03"));
- printColl(al);
- System.out.println();
- ArrayList<Student> al1 = new ArrayList<Student>();
- al1.add(new Student("abc01"));
- al1.add(new Student("abc02"));
- al1.add(new Student("abc03"));
- printColl(al1);
- }
- public static void printColl(ArrayList<? extends Person> al)
- {
- Iterator<? extends Person> it = al.iterator();
- while(it.hasNext())
- {
- System.out.println(it.next().getName());
- }
- }
- }
复制代码
|