黑马程序员技术交流社区
标题:
求解答:instanceof的问题
[打印本页]
作者:
智远
时间:
2015-4-20 15:26
标题:
求解答:instanceof的问题
public int compareTo(Object o) {
if(!(o instanceof Person)){
throw new RuntimeException("不是Person");
}
Person p=(Person) o;
if (this.age>p.age) {
return 1;
}
if (this.age==p.age) {
return this.name.compareTo(p.name);
}
return -1;
}
复制代码
TreeSet ts=new TreeSet();
ts.add("2");
ts.add(new Person("d",2));ts.add(new Person("c",2));
我加了一个不是Person的对象,为什么没输出这一句: "不是Person"
作者:
一个好人
时间:
2015-4-20 15:27
我觉得是if else if .else这样写
作者:
智远
时间:
2015-4-20 15:31
一个好人 发表于 2015-4-20 15:27
我觉得是if else if .else这样写
不是这个关系,第一个if中我传一个“2”对象时,括号的结果应该是true才对,但是if里面的语句却没执行
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2