编译没错,但是运行就出错了!何解?
代码如下:class A
{
public void fun1()
{
System.out.println("A fun1 is calling");
}
public void fun2()
{
fun1();
}
}
class B extends A
{
public void fun1()
{
System.out.println("B fun1 is calling");
}
public void fun3()
{
System.out.println("B fun3 is calling");
}
}
class C
{
public static void main(String [] args)
{
B b= new B();
callA(b);
A a=b;
callA(a);
callA(new A());//我在这里引入一个A类的对象
}
public static void callA(A a)
{
B b=(B)a ;//这里就不能强制转换了,但是编译时没错的!!
b.fun1();
b.fun2();
b.fun3();
}
}
求高手指点!!