import java.util.*;
class GenericDemo1
{
public static void main(String[] args)
{
TreeSet<Student> s=new TreeSet<Student>(new Comp());
s.add(new Student("aaa",23));
s.add(new Student("ccc",24));
s.add(new Student("bbb",25));
Iterator<Student> it=s.iterator();
while(it.hasNext())
{
System.out.println(it.next().getName());
}
}
}
class Person
{
private String name;
private int age;
Person(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name;
}
}
class Student extends Person
{
Student(String name,int age)
{
super(name,age);
}
}
class Comp implements Comparator<Person>
{
public int compare(Person p1,Person p2)
{
return p1.getName().compareTo(p2.getName());
}
}
为什么写成Iterator it=s.iterator();下面的就编译不了啊?有点不明白请帮忙解释一下,谢谢.
System.out.println(it.next().getName());这一句编译的时候提示找不到getName方法?
为什么写成System.out.println(it.next());编译就能通过呢? |
|