当前位置: 代码迷 >> J2SE >> 关于((Comparable)otherObject).compareTo(this);解决方案
  详细解决方案

关于((Comparable)otherObject).compareTo(this);解决方案

热度:295   发布时间:2016-04-24 16:49:53.0
关于((Comparable)otherObject).compareTo(this);
core   java书中的一个程序片断:
其中Employee为Manager的超类
二者都实现了接口Comparable,即  

public   interface   Comparable
{
    int   compareTo(Object   otherObject);
}

下面是Manager中实现的compareTo方法
public   int   compareTo(Object   otherObject)
{
    if(otherObject   instanceof   Manager)
    {
        Manager   other=(Manager)otherObject;
    }
   
    else   if(otherObject   instanceof   Employee)
    {
        return   1;
    }
   
    else
        return   -((Comparable)otherObject).compareTo(this);
}

我想问的是:最后一个return的调用的compareTo是那个类实现的


------解决方案--------------------
这个方法应该是基类实现的,既Object根类
------解决方案--------------------
个人觉得这样写,明显是有问题的
otherObject不总是可以转成Comparable对象,即不能保证传入的参数肯定实现了Comparable接口
  相关解决方案