3
假设我有这三类:我可以从super超级方法调用重写的方法吗?
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
类型的对象。
这是甚至编译? – sll
当然!问题是什么?? –
我忽略了这些方法是私人的 – sll