import java.util.*;
class GenericDemo1
{
public static void main(String[] args)
{
ArrayList<Person> al = new ArrayList<Person>();
al.add(new Person("zhangsan"));
al.add(new Person("lisi"));
al.add(new Person("wangwu"));
//printColl(al);
ArrayList<Student> al1 = new ArrayList<Student>();
al1.add(new Student("abc1"));
al1.add(new Student("abc2"));
al1.add(new Student("abc3"));
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());
}
}
}
class Person
{
private String name;
private int age;
Person(String name)
{
this.name = name;
//this.age = age;
}
public String getName()
{
return name;
}
}
class Student extends Person
{
private String name;
Student(String name)
{
super(name);
}
public String getName()
{
return name;
}
}
以上这段代码打印的结果是三个null,有哪位知道原因的高手帮忙详细解答一下原因,感谢!
|
|