本帖最后由 韩新凯 于 2012-5-8 16:09 编辑
徐然 发表于 2012-5-8 13:15
class Comp implements Comparable{//
毕老师说是可以的:
package com.generic;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class GenericDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new Comp());
ts.add(new Student("abc1"));
ts.add(new Student("abc2"));
ts.add(new Student("abc3"));
Iterator<Student> it = ts.iterator();
while (it.hasNext()) {
Student stu = it.next();
System.out.println(stu.getName());
}
}
}
class Person {
private String name;
public Person(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Student extends Person {
public Student(String name) {
super(name);
// TODO Auto-generated constructor stub
}
}
class Comp implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
// TODO Auto-generated method stub
return o1.getName().compareTo(o2.getName());
}
}
|