Fragment中的getActivity()vs this.getActivity()

Cho*_*ski 1 android android-context android-fragments

我在Android中是新的,我想知道是否有之间的差异getActivity(),并this.getActivity()在片段clases.例如,我们在siple类中有一个方法(不扩展Activity或Fragment),如:

 public static void method(Context context){

... some code

    }
Run Code Online (Sandbox Code Playgroud)

如果我们想要使用它,只需在我们的片段类中调用它:

MyMethodClass.method(getActivity());
Run Code Online (Sandbox Code Playgroud)

要么

MyMethodClass.method(this.getActivity());
Run Code Online (Sandbox Code Playgroud)

我知道两者都有效,但我需要专业的意见.

谢谢.

小智 7

他们是一样的.this关键字引用当前对象.

public class Car {
    int speed = 10;

    public void move() {
        //using this.speed or speed makes no difference here
    }
}
Run Code Online (Sandbox Code Playgroud)