Java比较接口compareTo方法
我没有发现我做错了什么,但 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它的拼写为
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 usinginstanceof
at all. There's a thorough section on writing Comparable types in the Object Ordering Java tutorial.