| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2082 人关注过本帖
标题:错误: 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
帖 子:2277
专家分: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
快速回复:错误: Student不是抽象的, 并且未覆盖Comparable中的抽象方法compar e ...
数据加载中...
 
   



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

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