-27

道歉:- 由于我的错误,因为上次我没有正确地问这个问题,这就是为什么下面发布的大多数答案都与 “this”关键字有关,这就是我得到那么多反对票的原因。所以我更新了这个问题,因为我不想误导任何人。

.

编辑-1:

问题 1 我的问题是为什么我们在GestureDetectorCompact()构造函数中两次传递“this”(当前类或 MainActivity 的对象)

 new GestureDetectorCompat(this,this);

其余代码块如下所示,

public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
  protected void onCreate(){
      private GestureDetectorCompat gestureDetector;
      this.gestureDetector = new GestureDetectorCompat(this,this);
      gestureDetector.setOnDoubleTapListener(this);

   }
}

EDIT-2:对于非常接近的答案,您可以单击以下链接

EpicPandaForce 的回答非常接近我的问题,也很有帮助。

4

6 回答 6

3

存在于许多 OOP 语言中的this关键字是对包含在内存中的对象的当前实例的引用。

你的例子:

this.gestureDetector = new GestureDetectorCompat(this,this);

您基本上是在说:此实例-访问gestureDetector等于GestureDetectorCompat使用 2 个参数构造的新实例,在这种情况下,它们都引用了MainActivity.

正如人们所说,这是一个基本原则,在转向 Android 之前,先在 Java 中打下坚实的基础可能对您更有利。

于 2015-08-19T16:42:07.630 回答
1

如果你想明白this,或许你应该读读 这个

Android 是一个基于 Java 的平台,需要良好的 Java/OOP 知识才能制作(本机)应用程序。

我强烈建议您至少阅读一本 Java/OOP 书籍/教程。一些很好的例子是:

好好学习!

于 2015-08-25T20:22:40.507 回答
0
public class MainActivity 
    extends 
          ActionBarActivity 
    implements 
          GestureDetector.OnGestureListener, 
          GestureDetector.OnDoubleTapListener {

  private Context context;
  private GestureDetector.OnGestureListener onGestureListener;
  private GestureDetector.OnDoubleTapListener onDoubleTapListener;

  private GestureDetector gestureDetector;

  protected void onCreate(Bundle saveInstanceState){
      super.onCreate(saveInstanceState);
      this.context = this; //mainActvity instance as a context
      this.onGestureListener = this; //mainActvity instance as GestureDetector.OnGestureListener
      this.onDoubleTapListener = this; //mainActvity instance as GestureDetector.OnDoubleTapListener

      //the gesture detector of this activity instance
      this.gestureDetector = new GestureDetectorCompat(context, onGestureListener);
                                            //activity as context
                                                  //activity as onGestureListener
      gestureDetector.setOnDoubleTapListener(onDoubleTapListener);
                                              //activity as double tap listener
   }
}

有关更多信息,请阅读GestureDetectorCompat 此处的构造函数

于 2015-08-14T07:16:18.817 回答
0

GestureDetectorCompat 构造函数的定义是,

GestureDetectorCompat(GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener) { }

所以调用这个函数需要 2 个参数。在您的情况下, MainActivity 正在实现两个 Listener 接口,这就是为什么您需要在两个参数中传递相同的引用来处理两个接口各自的回调。

在其他一些实现中,这两个接口也可以单独处理。

于 2018-06-05T16:30:01.810 回答
0

这里,

this.gestureDetector = new GestureDetectorCompat(this,this);

第一个“this”指的是“context”,第二个“this”指的是“listener”。

你可以看看这个,

public GestureDetectorCompat(Context context, OnGestureListener listener)      {
    this(context, listener, null);
}

GestureDetectorCompat 是我们在其中传递两个“this”关键字的构造函数,这意味着第一个是上下文(指当前类的对象),第二个是侦听器。

于 2017-03-18T11:34:53.690 回答
-2

this

在实例方法或构造函数中,this 是对当前对象的引用——调用其方法或构造函数的对象。您可以使用 this 从实例方法或构造函数中引用当前对象的任何成员。

这里this指的是当前Activity调用MainActivity

于 2015-08-14T07:16:44.613 回答