import java.util.*;
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;
}
}
class Student extends Person
{
Student(String name,int age)
{
super(name,age);
}
}
class Worker extends Person
{
Worker(String name,int age)
{
super(name,age);
}
}
class Fanxing
{
public static void main(String[] args)
{
TreeSet<Student> stu=new TreeSet<Student>(new Comp());//传一个比较器
stu.add(new Student("xuesheng1",16));
stu.add(new Student("xuesheng3",19));
stu.add(new Student("xuesheng2",23));
TreeSet<Worker> wor=new TreeSet<Worker>(new Comp());
wor.add(new Worker("gongreng1",34));
wor.add(new Worker("gongreng2",35));
wor.add(new Worker("gongreng3",39));
Iterator<Student> in1=stu.iterator();
while(in1.hasNext())
{
System.out.println(in1.next().getName());
}
Iterator<Worker> in2=wor.iterator();
while(in2.hasNext())
{
System.out.println(in2.next().getName());
}
}
}
class Comp implements Comparator<Person>//比较器
{
public int compare(Person p1,Person p2)
{
return p1.getName().compareTo(p2.getName());
}
}
|