Java比较接口compareTo方法

发布于 2024-08-23 20:45:10 字数 503 浏览 4 评论 0原文

我没有发现我做错了什么,但 NetBeans 给了我以下错误:

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");
    }
}

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");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2024-08-30 20:45:11

它的拼写为instanceof

另外,正如 Tom Hawtin 在评论中提到的,如果您使用 Java 1.5 或更高版本,您可以编写 compareTo(Employee emp) 来完全避免使用 instanceof。在对象排序 Java 教程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文