public static void main(String[] args) {
   try{
     test t = new test();    
     ( (A) t);  
   }
  catch(Exception e)
  {
    System.out.println("there is a exception"+e.getMessage());  }
  }}
class A
    extends test {  void A() {
    System.out.println("AA");
  };}
exception:ClassCastException
如果改写成test = new A()就可以了为什么.....................是不是栈区和堆区的问题???????/

解决方案 »

  1.   

    ( (A) t);    //好想这样不会调用A 类的构造
    test = new A() //调用了
    -------反正这样转换不安全。
      

  2.   

    感觉你对基类和继承类的关系,没搞清楚。
    test t = new test();
    (A) t;---> 这是错误的!!
    不能这样类型转换。
      

  3.   

    帮你改了一下,如下:
    package com;class test
    {
        
    }
    class A extends test {  A() {
        System.out.println("AA");
      }
      public static void main(String[] args) {
         try{
           A t = new A();    
           test B=(test)t;  //可以强制转化成父类成员
         }
        catch(Exception e)
        {
          System.out.println("there is a exception"+e.getMessage());
      
        }
      }
    }