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()就可以了为什么.....................是不是栈区和堆区的问题???????/
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()就可以了为什么.....................是不是栈区和堆区的问题???????/
test = new A() //调用了
-------反正这样转换不安全。
test t = new test();
(A) t;---> 这是错误的!!
不能这样类型转换。
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());
}
}
}