Bob*_*Bob 141 java this anonymous-class
给出以下代码:
public interface Selectable {
public void select();
}
public class Container implements Selectable {
public void select() {
...
}
public void createAnonymousClass() {
Selectable s = new Selectable() {
public void select() {
//see comment below.
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
我想Container.select()
从我的匿名类' select()
方法中访问.但是,this.select()
会再次调用匿名类的select()
方法.
我的建议是:
在Container中引入一个字段,例如
private Container self = this;
Run Code Online (Sandbox Code Playgroud)
现在我可以Container.select()
通过self.select()
在匿名类中调用来访问.
这是一种合理的方式吗?还是有更好的方法吗?
Myk*_*yev 263
Container.this.select();
Run Code Online (Sandbox Code Playgroud)