提供finish()
と方法this.finish()
は同じですかonPause()
?onStop()
5 に答える
はい。の意味をよく理解してくださいthis.
-> その値は現在のオブジェクトへの参照です。たとえば、 という名前のクラスがあり、 という名前Foo
のメソッドがある場合、method()
そのthis
中に のインスタンスFoo
(つまり、Foo
オブジェクト) への参照があります。通常は使用する必要はありませんthis
。
質問は3歳ですが、現在および将来の研究者に光を当てることを好みます.
this
は単なるオブジェクト参照ですthis
。子クラスのインスタンスから親クラスの参照を取得する必要がある場合を除いて、毎回使用する必要はありません。
Thread
クラスを使用する場合の例を考えてみましょう。
public class A
{
public A()
{
new Thread(new Runnable()
{
public void start()
{
B child=new B(A.this);//In this scenario,'A.this' refers to the parent class 'A' in which the 'Thread' class instantiated.If you simply pass 'this' ,then it would refer to the 'Thread' class as this statement executed in the current scope.
}
}).start();
}
}
public class B
{
A parent;
public B(A parent)
{
this.parent=parent;//'this' refers to the class B ,so that it can access the global variable 'parent' ,then assigns it with the local variable 'parent' passed through the constructor.
}
}
上記のように、キーワードにはさまざまな使用法があります。this
ここでオラクルのドキュメントを参照することをお勧めしますhttps://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
this
どのコンテキストでも、含まれているクラスを指します。したがって、 内でメソッドを使用している場合Activity
はthis.finish()
と同じですが、別のクラス タイプでfinish().
使用している場合は、this
this.finish()
あなたの場合は同じです。次の例のように、同じ名前のメンバーとメソッド パラメーターがある場合は、this->... を使用することが重要な場合があります。
class foo{
int number;
void setNumber(int number);
}
だからあなたはあなたの方法で書くことができます
void foo::setNumber(int number)
{
this->number = number;
}
したがって、どの要素を使用したかは明らかです。ただし、同じ名前を使用しないように注意してください。あまり良くありません。
finish()
とthis.finish()
同じです。
質問の他の部分については、アクティビティのライフサイクルについてお読みください。