给出以下代码:
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.
}
};
}
}
我想从我的匿名类的select()方法中访问Container.select()。然而,this.select()将再次调用匿名类的select()方法。
我的建议是:
将字段引入Container,例如
private Container self = this;
现在我可以通过从匿名类中调用self.select()访问Container.select()。
这是一个合理的方法吗?还是有更好的方法吗?