在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' ... –