3

假设我们有一些Foo类包含

public void setOnSomethingListener(OnSomethingListener listener);

我们注册它:

Foo foo = new Foo();
foo.setOnSomethingListener(new OnSomethingListener(){
  public void onSomething(String data) {
    // ...
  }
});

是否可以从onSomething已注册提到的侦听器的方法父对象访问而不访问定义的foo值?

我询问它是因为我想将此侦听器分配给某个参数并将其注册到少数对象中。

4

2 回答 2

7

Yes.

What you are doing here is defining an anonymous inner class. You can access attributes and methods of the containing class by doing.

MyContainingClass.this.attributeName
于 2012-07-17T12:42:28.187 回答
0

Try

OnSomethingListener.this.*
于 2012-07-17T12:41:26.830 回答