简体   繁体   中英

Java Comparable Interface compareTo method

I don't see anything that I am doing wrong, but NetBeans gives me the following error:

incomparable types
required: boolean
found: java.lang.Object


public int compareTo(Object obj)  {
    if( obj instaceof Employee){
       Employee employee = (Employee) obj;
       if(this.weekly_earnings > employee.weekly_earnings)
           return 1;
       else if(this.weekly_earnings == employee.weekly_earnings)
           return 0;
       else
           return -1;
    }
    else{
        System.out.println("Error");
    }
}

It's spelled instanceof .

Also, as Tom Hawtin mentioned in a comment, if you're using Java 1.5 or later you can write compareTo(Employee emp) to avoid using instanceof at all. There's a thorough section on writing Comparable types in the Object Ordering Java tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM