标题: 异常抛出的问题 [打印本页] 作者: 好大一只 时间: 2016-2-3 14:48 标题: 异常抛出的问题 public class Demo {
public static void main(String[] args)
{
R r1=new R(20);
R r2=new R(20);
D d=new D();
System.out.println(r1.equals(r2));
System.out.println(r2.equals(d));
}
}
class R
{
private int age;
R(int age)
{
this.age=age;
}
public boolean equals(Object obj)
{
if(!(obj instanceof R))
throw new ClassCastException("类型错误");
R r=(R)obj;
return this.age==r.age;
}
}
class D
{
}
显示
Exception in thread "main" true
java.lang.ClassCastException: 类型错误
at R.equals(aaaa.java:21)
at Demo.main(aaaa.java:8)