【发布时间】:2012-04-02 03:58:44
【问题描述】:
有没有办法访问匿名外部类? ClassName.this 可以访问一个普通的类。这不起作用,因为匿名类显然没有名称。我也尝试使用扩展类/接口(如 Runnable.this),但它似乎不会以这种方式工作。
我确信这可能不是最好的编码风格,我只是好奇是否可以不将外部的这个存储在变量中。
例如,注意outer.this:
public class A
{
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (outher.this) {
outher.this.notify();
}
}
}).start();
try {
synchronized (this) {
wait();
}
} catch (final InterruptedException ex) {}
}
}).start();
}
}
【问题讨论】:
标签: java syntax anonymous-class