黑马程序员技术交流社区
标题:
帮忙看看这个代码怎么编译不通过
[打印本页]
作者:
谭荣强
时间:
2014-3-8 11:46
标题:
帮忙看看这个代码怎么编译不通过
提示找不到getName()
import java.util.*;
class fanxing
{
public static void main(String[] args)
{
//ArrayList<Person> a = new ArrayList<Person>();
//a.add(new Person("lixi3"));
//print(a);
ArrayList<Student> a1 = new ArrayList<Student>();
a1.add(new Student("abc1"));
a1.add(new Student("abc2"));
print(a1);
}
public static void print(ArrayList<? super Student> a )
{
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
{
Student(String name)
{
super(name);
}
}
作者:
osully
时间:
2014-3-8 11:55
修改方式如下:
<? super Student>改为<? extends Person>
import java.util.*;
public class Test1
{
public static void main(String[] args)
{
//ArrayList<Person> a = new ArrayList<Person>();
//a.add(new Person("lixi3"));
//print(a);
ArrayList<Student> a1 = new ArrayList<Student>();
a1.add(new Student("abc1"));
a1.add(new Student("abc2"));
print(a1);
}
public static void print(ArrayList<? extends Person> a )
{
Iterator<? extends Person> 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
{
Student(String name)
{
super(name);
}
}
复制代码
作者:
syw02014
时间:
2014-3-8 12:00
把print函数中的super改换成extends就ok了;你的源代码已修改:
class fanxing
{
public static void main(String[] args)
{
//ArrayList<Person> a = new ArrayList<Person>();
//a.add(new Person("lixi3"));
//print(a);
ArrayList<Student> a1 = new ArrayList<Student>();
a1.add(new Student("abc1"));
a1.add(new Student("abc2"));
print(a1);
}
public static void print(ArrayList<? extends Student> a )
{
Iterator<? extends 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
{
Student(String name)
{
super(name);
}
}
复制代码
作者:
syw02014
时间:
2014-3-8 12:06
把你的注释去掉后就运行这个(print可以打印Person和Student的对象):
class fanxing
{
public static void main(String[] args)
{
ArrayList<Person> a = new ArrayList<Person>();
a.add(new Person("lixi3"));
print(a);
ArrayList<Student> a1 = new ArrayList<Student>();
a1.add(new Student("abc1"));
a1.add(new Student("abc2"));
print(a1);
}
public static void print(ArrayList<? extends Person> a )
{
Iterator<? extends Person> 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
{
Student(String name)
{
super(name);
}
}
复制代码
作者:
王松松
时间:
2014-3-8 13:15
本帖最后由 王松松 于 2014-3-8 13:24 编辑
额....没注意看你加了泛型,那就不需要强转了Iterator<E>,好像没有泛型上下限,你换成Iterator<Student>看看
作者:
谭荣强
时间:
2014-3-8 16:17
明白了 thanks
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2