30

私の先生は、メソッド内のインスタンス変数にアクセスしようとするときは、常にthisキーワードを使用する必要があると言います。そうしないと、二重検索を実行することになります。ローカル スコープ検索とインスタンス スコープ検索。

例:

public class Test(){
    int cont=0;
    public void Method(){
        System.out.println(cont);//Should I use This.cont instead?
    }
}

彼が間違っていることを願っていますが、議論が見つかりません。

4

8 に答える 8

40

いいえ、thisメソッド パラメーターが設定しているインスタンス フィールドと同じ名前を持つ場合など、名前の競合がある場合にのみ使用してください。

それ以外の場合にも使用できますが、私たちの多くは、コードに不必要な言葉遣いを追加するだけだと感じています。

于 2013-04-05T03:51:31.003 に答える
25

名前の競合のため、必要に応じて使用する必要がありますが、それらを完全に回避することをお勧めします。this

希望すれば使えます。thisそれは純粋に好みの問題です。

先生が要求した場合は、学業で使用する必要があります。this

于 2013-04-05T03:55:55.653 に答える
2

thisインスタンス内の現在のインスタンスのエイリアスまたは名前です。ローカル (パラメーターを含む) からインスタンス変数を明確にするのに役立ちますが、単にメンバー変数とメソッドを参照したり、他のコンストラクターのオーバーロードを呼び出したり、単にインスタンスを参照したりするために単独で使用できます。Java - いつ「this」キーワードを使用するかを
参照してください。また、これは現在のオブジェクトを参照します。変数 int A を持つクラスがあり、クラスのメソッド xyz 部分に int A がある場合、参照している「A」を区別するために、this.A を使用します。これはほんの一例です。

public class Test
{
int a;

public void testMethod(int a)
{
this.a = a;
//Here this.a is variable 'a' of this instance. parameter 'a' is parameter.
}
}

したがって、このキーワードは次の目的で使用できると言うかもしれませ
ん (静的メソッドでは使用できません)。

        1)To get reference of an object through which that method is 
        called within it(instance method).
        2)To avoid field shadowed by a method or constructor parameter.
        3)To invoke constructor of same class.
        4)In case of method overridden, this is used to invoke method of current class.
        5)To make reference to an inner class. e.g ClassName.this
于 2013-04-05T03:53:33.657 に答える
1

誰もが名前のあいまいさを解消する例を挙げているので、thisヘルプを使用する場合の例を示します。

public class Person {
    private final firstName;
    private final lastName;
    private final Date birthdate;
    private final Address address;

    @Override
    public boolean equals(Object otherObject) {
        if (!(otherObject instanceof Person) {
            return false;
        }

        Person otherPerson = (Person) otherObject;

        // Using this here help distinguishing the current instance and the other.
        return this.firstName.equals(otherPerson.firstName)
            && this.lastName.equals(otherPerson.lastName)
            && this.birthdate.equals(otherPerson.birthDate)
            && this.address.equals(otherPerson.address);
    }

}
于 2013-04-05T04:00:04.987 に答える
0

thisパラメーターがクラス プロパティと同じ名前を持つ場合にのみ適用されます。

public class Dog {
   String name;
   public Dog(String name) {
      name = name; //but which name? Are we just assigning a variable to itself?
      // here you could say this.name = name. Or you could rename one of the variables to resolve ambiguity
   }
}
于 2013-04-05T03:52:06.210 に答える
0

thisオートコンプリート (作業が楽になる) のために時々使用しますが、後でクリーンアップします。

明確さが重要であることを忘れないでください。この場合、contがクラス変数であることは明らかですが、インスタンス変数が積み重なった巨大なクラスを作成している場合はthis、わかりやすくするために を使用することを検討してください。

また、名前の衝突がある場合にのみ使用する必要があります。this

public class Ex {
    int num;
    public Ex(int num) {
        this.num = num; 
    }
}

この例では、num = num は衝突を引き起こしますが、"this" はそれを回避します。これは絶対に必要な唯一の場合ですが、多くの場合、明快さが優先されます。

于 2013-04-05T03:52:14.633 に答える