在Java中,我使用扩展B的类A中的匿名类。如何从这个匿名类访问B?我不能使用关键字super
,因为这意味着超类的匿名类,而不是超类的A。Java:从匿名类获取超类
public class A {
void foo() {
System.out.println("Good");
}
}
public class B extends A {
void bar() {
Runnable r = new Runnable() {
@Override
public void run() {
foo(); // Bad: this call B.foo(), not A.foo()
// super.foo(); // Bad: "Method foo is undefined for type Object"
}
};
r.run();
}
@Override
void foo() {
System.out.println("Bad");
}
}
我怀疑你能做到这一点,考虑到类只有*引用*到'B' ... –