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());
}
}
}
为什么会编译失败啊 |
|