A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

import java.util.*;
class User implements Comparable
{
   private String name;
   private int age;
   User(String name,int age)
   {
        this.name=name;
        this.age=age;
   }
   public String getName()
  {
        return name;
  }
   public int getAge()
  {
        return age;
  }
  public int compareTo(object obj)
  {
         if(!(obj instanceof User )){
               throw new RuntimeException(" 不是User类对象");
           }
          User s=(User)obj;
         if(this.age<s.age)
              return 1;
         if(this.age==s.age)
              return this.name.compareTo(s.name);
          return -1;
  }  
}


public class test{
          public static void main(String[] args){
                   ArrayList<User> us=new ArrayList<User>();
                   us.add(new User("李洋",19));
                   us.add(new User("李新",18));
                   us.add(new User("李雨",20));
                   us.add(new User("李海",17));
                   us.add(new User("李墨",26));
                  
                   Iterator<User> it=us.iterator();
                   while(it.hasNext()){
                            User lx=(User)it.next();
                             System.out.println(lx);
                  }
          }
}


运行时报错,求大神解答

5 个回复

倒序浏览
   可以把报错信息贴出来看看么?那样的话更直观吧。
回复 使用道具 举报
C:\Documents and Settings\Administrator\桌面

点评

禁止水贴!  发表于 2015-5-11 17:51
回复 使用道具 举报
图片怎么上传

飞信截图20150511150347.jpg (71.55 KB, 下载次数: 8)

飞信截图20150511150347.jpg
回复 使用道具 举报
覆盖compareTo方法的时候Object的O要大写。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马