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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© YC匪帮 中级黑马   /  2015-9-28 10:51  /  541 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1.         public int compare(Object o1, Object o2) {
  2.                 if(o1 instanceof Student1 & o2 instanceof Student1){
  3.                         Student1 s1 = (Student1)o1;
  4.                         Student1 s2 = (Student1)o2;
  5.                         if(s1.getAge() > s2.getAge()){
  6.                                 return -1;
  7.                         }else if(s1.getAge() < s2.getAge()){
  8.                                 return 1;
  9.                         }
  10.                 }
  11.                 return 0;
  12.         }
复制代码

这段代码里如果o1或者o2不是student1的对象,是不是就返回它俩个相等啊?
有点晕乎了.....

3 个回复

倒序浏览
返回0啊,看你怎么使用标记了,
回复 使用道具 举报
如果不是student对象会返回  异常  castException    没有student怎么调用compare?
回复 使用道具 举报
boboyuwu 发表于 2015-9-28 12:28
如果不是student对象会返回  异常  castException    没有student怎么调用compare?

那if还有个毛用?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马