本帖最后由 闲人 于 2014-4-2 11:49 编辑
下面代码运行错误,看了好久,看不出哪里错了,代码提示我已在下面标注,希望有知道的指点我下
------------------------
import java.util.*;
public class GenericDemo6 {
public static void main(String[] args) {
ArrayList<Person> al = new ArrayList<Person>();
al.add(new Person("abc1"));
al.add(new Person("abc2"));
al.add(new Person("abc3"));
ArrayList<Student> al1 = new ArrayList<Student>();
//说是下面一行有问题,但是错误在最下面的类
al1.add(new Student("abc---1"));
al1.add(new Student("abc---2"));
al1.add(new Student("abc---3"));
printlnColl(al1);
}
public static void printlnColl(ArrayList<? extends Person> al)
{
Iterator<? extends Person> it = al.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;
}
}//显示The type Student is already defined,
//但我看不出怎么错了,怎么改,如果可以麻烦告诉我为什么
class Student extends Person
{
Student(String name)
{
super(name) ;
}
}
|