- import java.util.*;
- class GenericDemo6
- {
- public static void main(String[] args)
- {
- ArrayList<Person> ps = new ArrayList<Person>();
- ps.add(new Person("abc1"));
- ps.add(new Person("abc2"));
- ps.add(new Person("abc3"));
- //printColl(ps);
-
- ArrayList<Student> stu = new ArrayList<Student>();
- stu.add(new Student("abc---1"));
- stu.add(new Student("abc---2"));
- stu.add(new Student("abc---3"));
-
- printColl(stu);//ArrayList<Person> ps = new ArrayList<Student>();error
-
- }
- public static void printColl(ArrayList<?> ps)
- {
- Iterator<?> it = ps.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);
- }
- }
复制代码
运行报错 ,是真么回事? |
|