本帖最后由 张向辉 于 2013-2-6 15:11 编辑
package a;
public class Polymorphism {
public static void main(String[] args) {
A b = new B();
b.fb();
}
}
class A {
public A() {
}
public void fa() {
System.out.println("CLASS A :Function fa Runing......");
}
public void fb() {
System.out.println("CLASS A :Function fb Runing......");
fa();
System.out.println("CLASS A :Function fb Stop......");
}
}
class B extends A {
public B() {
}
public void fa() {
System.out.println("CLASS B :Function fa Runing......");
}
public void fb() {
System.out.println("CLASS B :Function fb Runing......");
super.fb();
System.out.println("CLASS B :Function fb Stop......");
}
}
结果为什么是:
CLASS B :Function fb Runing......
CLASS A :Function fb Runing......
CLASS B :Function fa Runing......
CLASS A :Function fb Stop......
CLASS B :Function fb Stop......
也就说:super.fb();为什么执行B中的fa(),而不是A中的fa().别告诉我是,方法覆盖。 |