黑马程序员技术交流社区

标题: 16天的6为啥不写泛型也不会报友好提示呢 [打印本页]

作者: BitmapFactory    时间: 2013-3-15 21:36
标题: 16天的6为啥不写泛型也不会报友好提示呢
  1. import java.util.*;
  2. class Student implements Comparable<Student>
  3. {
  4.         private String name;
  5.         private int age;
  6.         Student(String name,int age)
  7.         {
  8.                 this.name = name;
  9.                 this.age = age;
  10.         }
  11.         public int compareTo(Student s)
  12.         {
  13.                 int num = new Integer(this.age).compareTo(new Integer(s.age));
  14.                 if(num==0)
  15.                         return this.name.compareTo(s.name);
  16.                 return num;
  17.         }
  18.         public int hashCode()
  19.         {
  20.                 return name.hashCode() + age*34;
  21.         }
  22.         public boolean equals(Object obj)
  23.         {
  24.                 if(!(obj instanceof Student))
  25.                         throw new ClassCastException("类型不匹配");
  26.                 Student s = (Student)obj;

  27.                 return this.name.equals(s.name) && this.age == s.age;
  28.         }
  29.         public String getName()
  30.         {
  31.                 return name;
  32.         }
  33.         public int getAge()
  34.         {
  35.                 return age;
  36.         }
  37.         public String toString()
  38.         {
  39.                 return name + ":" +age;
  40.         }
  41. }
  42. class MapTest
  43. {
  44.         public static void main(String[] args)
  45.         {
  46.                 HashMap<Student,String> hm = new HashMap<Student,String>();

  47.                 hm.put(new Student("lisi1",21),"beijing");
  48.                 hm.put(new Student("lisi2",22),"shanghai");
  49.                 hm.put(new Student("lisi3",23),"nanjing");
  50.                 hm.put(new Student("lisi4",24),"wuhan");

  51.                 Set<Student> keySet = hm.keySet();
  52.                 Iterator<Student> it = keySet.iterator();
  53.                 while(it.hasNext())
  54.                 {
  55.                         Student stu = it.next();
  56.                         String addr = hm.get(stu);
  57.                         System.out.println(stu + "...." +addr);
  58.                 }

  59.                 Set<Map.Entry<Student,String>> entrySet = hm.entrySet();
  60.                 Iterator<Map.Entry<Student,String>> it1 = entrySet.iterator();
  61.                
  62.                 while(it1.hasNext())
  63.                 {
  64.                         Map.Entry<Student,String> me = it1.next();
复制代码
//68行这里为啥不写泛型<Student,String>也不会有JVM的友好提示呢


  1.                         System.out.println(me.getKey()+".."+me.getValue());
  2.                 }

  3.         }
  4. }
复制代码

作者: IT菜鸟    时间: 2013-3-15 22:09
在泛型中 参数化类型与原始类型有一定的兼容性;参数化类型可以引用一个原始类型的对象,原始类型也可以引用一个参数化类型。编译器应该是报警告,而不是报错。
作者: BitmapFactory    时间: 2013-3-15 23:13
蔡志刚 发表于 2013-3-15 22:09
在泛型中 参数化类型与原始类型有一定的兼容性;参数化类型可以引用一个原始类型的对象,原始类型也可以引 ...

还是没明白,蔡兄,能不能根据我的代码讲讲呢
作者: 谢洋    时间: 2013-3-16 01:23





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2