从侦听器访问对象 [英] Access an object from listener

查看:69
本文介绍了从侦听器访问对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一些Foo包含的类

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) {
    // ...
  }
});

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

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.

推荐答案

是.

您在这里正在定义一个匿名内部类.您可以通过这样做来访问包含类的属性和方法.

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

这篇关于从侦听器访问对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆