【发布时间】:2012-10-24 12:54:56
【问题描述】:
package main;
class F {
void f() {
System.out.print("F.f() ");
this.g();
}
void g() {
System.out.print("F.g() ");
}
}
class Fbis extends F {
void f() {
System.out.print("Fbis.f() ");
this.g();
}
void g() {
System.out.print("Fbis.g() ");
super.f();
}
}
public class Main {
public static void main(String[] args) {
F f = new Fbis();
((F) f).f();
}
}
您好,我想了解为什么 F 类中的 g() 函数从未被调用过。 这段代码可以编译并运行,但会导致一个无限循环,显示如下:
Fbis.f() Fbis.g() F.f() Fbis.g() F.f() Fbis.g() F.f() Fbis.g() F.f() Fbis.g() F.f() Fbis.g() F.f() Fbis.g() F.f() Fbis.g() F.f() ...
那么会发生什么,是调用了Fbis.f,它调用了Fbis.g,它调用了F.f,而不是调用F.g,F.f调用了Fbis.g。
【问题讨论】:
-
@FranzEbner 只有在他使用 Eclipse 时才有效;-)
-
@André Playbutton vs. Shell... Playbutton 获胜:D
-
@FranzEbner 当然。 Eclipse 很棒,我自己也在使用它。只是说:-)
标签: java class inheritance compilation