<<  n=n++; | 首 页 | 关于java基本数据类型  >>

java中父类和子类的强转问题。。。

[ JAVA ]

如下代码:

public class A {
 public static void main(String[] args){
  A a = new A();
  B b = (B)a;  //执行该语句会抛ClassCastException异常。
  
  B b2 = new B();
  a = (A)b2;
  System.out.println("success");
 }
}
class B extends A{ 
}

 

--子类由于比父类拥有了更多的特性,所以当你想将父类强制转化为其一个子类的时候,编译没有错误,但在运行时就会抛异常。反之,则可以。






so help me to get where i belong......i love u...forever....

评论

    发表评论

     姓名:
     E-mail:
     地址: