import java.util.*;
class GenericDemo6
{
public static void main(String[] args)
{
ArrayList<Person> al = new ArrayList<Person>();
al.add(new Person("abc2"));
al.add(new Person("abc3"));
al.add(new Person("abc4"));
al.add(new Person("abc5"));
ArrayList<Student> all = new ArrayList<Student>();
all.add(new Student("abc--2"));
all.add(new Student("abc--3"));
all.add(new Student("abc--4"));
all.add(new Student("abc--5"));
//print(al);
print(all);
}
public static void print(ArrayList<? super Student> a) //<? super E> :可以接收E类型或者E的父类类型。
{
Iterator<? super Student> it = a.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
{
private String name;
Student (String name)
{
super(name);
}
}
System.out.println(it.next().getName());
在这为什么提示找不到getName()方法呢? |