class A {
void fun1() {
System.out.println(fun2());
}
int fun2() {
return 123;
}
}
class B extends A
{
int fun2()
{
return 456;
}
public static void main(String[] args)
{
A a;
B b = new B();
b.fun1();//B是A的子类,调用fun1()方法,方法中涉及到fun2(),子类覆写父类,所以答案是456
a = b;//涉及到多态 A a=new B();父类的引用指向子类对象,运行时看右边,编译时看左边
a.fun1();
}
}
|