TreeSet(Comparator<? super E> comparator) TreeSet中有这么一个构造函数,在初始化时添加一个比较器,使用的就是<? super E> 这样的下限泛型限定
但是毕老师的代码实现中没发现使用下限限定,求解...
- import java.util.*;
- class TreeSetTest2
- {
- public static void main(String[] args){
-
- TreeSet<Person> pList=new TreeSet<Person>(new Comp());
- pList.add(new Person("abc126"));
- pList.add(new Person("abc127"));
- pList.add(new Person("abc123"));
- pList.add(new Person("abc124"));
- pList.add(new Person("abc125"));
- getColl(pList);
- }
- public static void getColl(TreeSet<? extends Person> pList){ //上限限定
- Iterator<? extends Person> it=pList.iterator();
- while(it.hasNext()){
- System.out.println(it.next().getName());
- }
- }
- }
- class Comp implements Comparator<Person> //如何实现下限限定???
- {
- public int compare(Person p1,Person p2){
- return p1.getName().compareTo(p2.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);
- }
- }
- class Worker extends Person
- {
- Worker(String name){
- super(name);
- }
- }
复制代码
|
|