黑马程序员技术交流社区

标题: 求教 [打印本页]

作者: 佛山java爱好者    时间: 2014-7-15 11:44
标题: 求教
class Student implements Comparable<Student>
{
        private String name;
        private int age;
        Student(String name,int age)
        {
          this.name=name;
          this.age=age;
        }
        public String getName()
        {
           return name;
        }
        public int getAge()
        {
          return age;
        }
        public String toString()
        {
          return name+":"+age;
        }
        public int hasCode()
        {
           return name.hasCode()+age*34;
        }
        public bollean equals(Object obj)
        {
          if(!(obj instanceof Student))
            throw new ClassCastException("类型不匹配");
          Student s=(Student)obj;
          return this.name.equals(s.name)&&this.age==s.age;
          
          }
          public int compareTo(Student s)
        {
          int num=new Integer(this.age.compareTo(new Integer(s.age)));
          if(num==0)
                  return this.name.compareTo(s.name);
              return num;
          
          }

}


class MapTest
{
        public static void main(String[] args)
        {
                HasMap<Student.String> hm=new HashMap<Student,String>();
                hm.put(new Student("lisi",21,"beijing"));
                hm.put(new Student("lisi1",22,"tianjing"));
                Set<Student> keySet=hm.keySet();
                Iterator<Student> it=keySet.iterator();
                while(it.hasNext())
                {
                  System.out.println(it.next());
                }
        }
}
为什么会编译失败啊
作者: 爨庚轩    时间: 2014-7-15 12:37
编译失败的错误提示是什么,
作者: 帮帮    时间: 2014-7-15 12:54
本帖最后由 帮帮 于 2014-7-15 12:57 编辑

HasMap<Student.String> hm=new HashMap<Student,String>();前后泛型内容不一致
Student.String 点
Student,String 逗号





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