泛型的限定中,<? extends Person>表示的是传入的是Person的子类都可以接收,那<? super Student>的是student的父类应该都可以接收了,可是出问题了。- import java.util.*;
- class Person
- {
- private String name;
- Person(String name)
- {
- this.name=name;
- }
- public String getName()
- {
- return name;
- }
- }
- class Student extends Person
- {
- //private String name;
- Student(String name)
- {
- super(name);
- }
- }
- class GenericDemo6
- {
- public static void main(String[] args)
- {
- ArrayList <Person>al=new ArrayList<Person>();
- al.add(new Person("ajfi"));
- al.add(new Person("fjia"));
- al.add(new Person("daff"));
- ArrayList <Student>al1=new ArrayList<Student>();
- al1.add(new Student("121"));
- al1.add(new Student("122"));
- al1.add(new Student("123"));
- print(al);
- print(al1);
- }
- public static void print(ArrayList <? super Student> al)
- {
- Iterator < ? super Student> it=al.iterator();
- while(it.hasNext())
- {
- System.out.println(it.next().getName());
- }
- }
-
- }
复制代码 编译提示找不到符号,求解? |