【发布时间】:2011-11-26 19:40:43
【问题描述】:
假设我有这三个类:
class Foo {
void fn() {
System.out.println("fn in Foo");
}
}
class Mid extends Foo {
void fn() {
System.out.println("fn in Mid");
}
}
class Bar extends Mid {
void fn() {
System.out.println("fn in Bar");
}
void gn() {
Foo f = (Foo) this;
f.fn();
}
}
public class Trial {
public static void main(String[] args) throws Exception {
Bar b = new Bar();
b.gn();
}
}
是否可以调用Foo 的fn()?我知道我在gn() 中的解决方案不起作用,因为this 指向Bar 类型的对象。
【问题讨论】:
-
这还能编译吗?
-
当然!!有什么问题??
-
我忽略了方法是私有的
标签: java inheritance polymorphism dynamic-binding