标题:错误: Student不是抽象的, 并且未覆盖Comparable中的抽象方法compar eTo(St ...
只看楼主
maojianxin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-11-13
结帖率:0
已结贴  问题点数:20 回复次数:2 
错误: Student不是抽象的, 并且未覆盖Comparable中的抽象方法compar eTo(Student)
import java.util.*;
class Student implements Comparable<Student>
{
    private String name;
    private int age;
    Student(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
   
    public int compareTO(Student s)
    {
        int num = new Integer(this.age).compareTO(new Integer(s.age));
        
        if(num==0)
            return this.(s.name);
        return num;
    }
   
    public int hashCode()
    {
        return name.hashCode()+age*34;
    }
    public boolean 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 String getName()
    {
        return name;
    }
    public int getAge()
    {
        return age;
    }
    public String toString()
    {
        return name+":"+age;
    }
   
}



class MapTest
{
    public static void main(String[] args)
    {
        HashMap<Student,String> hm = new HashMap<Student,String>();
        
        hm.put(new Student("lisi1"),"beijing");
        hm.put(new Student("lisi2"),"shanghai");
        hm.put(new Student("lisi3"),"nanjing");
        hm.put(new Student("lisi4"),"wuhan");
        
        //第一种取出方式 keySet
        
        Set<Student> keySet = hm.keySet();
        
        Iterator<Student> it = keySet.iterator();
        
        while(it.hashNext())
        {
            Student stu = it.next();
            String addr = hm.get(stu);
            System.out.println(stu+".."+addr);
        }
        
    }
}
搜索更多相关主题的帖子: Student String name age new 
2017-11-13 14:03
maojianxin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-11-13
得分:0 
在线等 新手 求大神指教!谢谢了
2017-11-13 14:06
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:20 

    public int compareTO(Student s)
    {
        int num = new Integer(this.age).compareTO(new Integer(s.age));
        
        if(num==0)
            return this.(s.name);
        return num;
    }
   

剑栈风樯各苦辛,别时冰雪到时春
2017-11-15 00:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-482428-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017877 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved