简体   繁体   中英

Access an object from listener

Let's say we have some Foo class that contains

public void setOnSomethingListener(OnSomethingListener listener);

We register it with:

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

Is it possible to access from onSomething method parental object that has registered mentioned listener without accessing defined foo value ?

I ask about it because I would like to assign this listener to some parameter and register it in the few objects.

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

尝试

OnSomethingListener.this.*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM