当我想引用方法中的实例变量时,我应该使用 "this" 关键字吗?



我的老师说,当我试图访问方法中的实例变量时,我应该始终使用this关键字,否则我会执行双重搜索。局部范围搜索,然后是实例范围搜索。

的例子:

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

我希望他是错的,但是我找不到任何论据

不,只有当您有名称冲突时才使用this,例如当方法参数与其正在设置的实例字段具有相同的名称时。

可以在其他时候使用,但是我们很多人觉得它只是给代码增加了不必要的冗余。

如果因为名称冲突而需要,必须使用this,尽管最好完全避免这些。

如果你愿意,你可以使用this。这纯粹是个品位问题。

如果老师要求的话,你应该在作业中使用this

this是实例内当前实例的别名或名称。它对于消除实例变量与局部变量(包括参数)的歧义很有用,但它本身也可以用来简单地引用成员变量和方法、调用其他构造函数重载或简单地引用实例。
参见Java -何时使用'this'关键字
另外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

你的老师是正确的,如果你不使用this关键字,它将导致双重搜索编译器。如果编译器无法在局部范围内找到变量,那么编译器将首先在局部范围内搜索,然后在实例范围内搜索。

另外,当编译器将代码转换为字节码时,编译器将在所有实例变量前加上this关键字。因此,如果您自己使用this关键字,您实际上减少了编译器的负担,代码将编译得更快。

既然每个人都给出了消除名称歧义的例子,我将给出一个使用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);
    }
}

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
   }
}

我偶尔使用this,因为自动完成(使生活更容易),但我之后清理它们。

记住,清晰是关键。在这种情况下,很明显cont是一个类变量,但是如果您正在编写一个包含大量实例变量的庞大类,那么为了清晰起见,您可能会考虑使用this

你也只需要使用this当有名称冲突时,例如

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

在这个例子中,num = num会导致冲突,而"this"避免了冲突。这是唯一绝对必要的时候,但同样,清晰往往是更高的优先级。

另一个经常使用this的地方是当内部类对象引用包含它的对象的字段时。

public class Foo {
    String foostring;
    /* snip lots of code */
    private class Foohelper {
        void helperMethod(String bazstring) {
             Foo.this.foostring = bazstring;
             // etc.
        }
    }
}

编译器不需要这个,但它使在哪里寻找foostring的情况更清楚。除了这个(!)之外,我只在构造函数中完全限定字段名,因为它们可能被参数名隐藏,正如许多其他海报在这里所说明的那样。

[编辑:现在我想到了,有编译器需要这个地方,例如,如果Foohelper.toString()想调用Foo.toString() .]

相关内容

最新更新