A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ò壞尛孩 中级黑马   /  2014-4-22 15:46  /  826 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

最近看到泛型的视频,有点点的小疑惑!泛型在C++里面  有点类似STL模板  不过C++里面是用Template关键字定义的
写法上没有JAVA的<>方便
  1. package liang;

  2. import java.util.*;
  3. class Person1  
  4. {
  5.          private String name;
  6.          private int age;
  7.          

  8.          Person1(String name,int age)
  9.          {
  10.                  this.name=name;
  11.                  this.age=age;
  12.          }
  13.         /* public int compareTo(Object obj)
  14.          {
  15.                  if(!(obj instanceof Person))
  16.                          throw new RuntimeException("参数不正确");
  17.                  
  18.                  Person p=(Person)obj;
  19.                  if(this.age>p.age)return 1;
  20.                  if(this.age==p.age)
  21.                          return this.name.compareTo(p.name);
  22.                  return -1;
  23.          }*/
  24.          
  25.          public String getName()
  26.          {
  27.                  return name;
  28.          }
  29.          public int getAge()
  30.          {
  31.                  return age;
  32.          }
  33.          public void show()
  34.          {
  35.                  System.out.println(this.name+"..."+this.age);
  36.          }
  37. }
  38. class Student1 extends Person1
  39. {
  40.          Student1(String name,int age)
  41.          {
  42.                  super(name,age);
  43.          }
  44. }
  45. class Comp implements Comparator<Person1>
  46. {
  47.         public int compare(Person1 a,Person1 b)
  48.         {
  49.                 return a.getName().compareTo(b.getName());
  50.         }
  51. }
  52. class GenericDemo
  53. {
  54.         public static void main(String[] args)
  55.         {
  56.                 TreeSet<Student1>s=new TreeSet<Student1>(new Comp());
  57.                
  58.                 s.add(new Student1("lisi00",21));
  59.                 s.add(new Student1("lisi01",21));
  60.                 s.add(new Student1("lisi02",21));
  61.                 s.add(new Student1("lisi03",21));
  62.                
  63.                 Iterator<Student1> it =s.iterator();
  64.                 while(it.hasNext())
  65.                 {
  66.                         it.next().show();
  67.                 }
  68.         }
  69. }
复制代码

毕老师说泛型限定分上限和下限
<? extends E>是上限,也就是可是接收E和E的子类类型
<? super E>是下限,也就是可是接受E和E的父类类型

TreeSet(Comparator<? super E> comparator)
          构造一个新的空 TreeSet,它根据指定比较器进行排序。
这不是泛型下限吗?Student继承Person,程序中Comparator比较器中传入的是Person类,应该是只能接受Person类和它的父类啊!为啥子类Student可以传
谁可以给我解释下这个extends和super上下限!

评分

参与人数 1技术分 +1 收起 理由
SyouRai_Tsk + 1

查看全部评分

2 个回复

倒序浏览
哥们 你确实很细心 发现这么多

不过这个很好理解
对应TreeSet<E>
Comparetor<T>
TreeSet(Comparator<? super E> comparator)

也就是T 是那个问号 比较器传入的类型当然要是存入TreeSet中的父类才可以
回复 使用道具 举报
osully 发表于 2014-4-22 20:30
哥们 你确实很细心 发现这么多

不过这个很好理解

谢谢大神解释!我懂了!
也就是 这个比较器Comparetor(T) 传入的类型  必须是 数据类型TreeSet(E)的父类或者自己的类型!而不是说只接受T和T的父类型是吧!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马