Java中this.variable和variable的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23529028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:37:18  来源:igfitidea点击:

Difference between this.variable and variable in Java

java

提问by jdorel

I don't understand the real difference between this two codes, despite the fact that they both work.

我不明白这两个代码之间的真正区别,尽管它们都有效。

If I use this class:

如果我使用这个类:

public class City {
    private String name;

I don't understand the difference between this method:

我不明白这种方法之间的区别:

public String getName(){
    return this.name;
}

And this one:

和这个:

public String getName(){
    return name;
}

The two methods work, but which one is the best to use, and why do they both work?

这两种方法都有效,但哪种方法最好用,为什么它们都有效?

Thanks for the answers.

感谢您的回答。

采纳答案by Alexandre Santos

"this" indicates the instance of the class.

“this”表示类的实例。

In the following example, "this.a" is the a defined in the class (=10). While "a" (=20) is the local variable, defined in the constructor.

在以下示例中,“this.a”是类中定义的 a (=10)。而 "a" (=20) 是局部变量,在构造函数中定义。

More details: http://en.wikipedia.org/wiki/Variable_shadowing

更多详情:http: //en.wikipedia.org/wiki/Variable_shadowing

Example:

例子:

public class Test
{
    int a = 10;
    public Test()
    {
        int a = 20;
        System.out.println(a); // prints 20
        System.out.println(this.a); // prints the "a" defined in the class. In this case, 10
    }

    public static void main(String[] args)
    {
        new Test();
    }
}

回答by nanofarad

The two methods are the same in most, but not all cases. Take the following constructor:

这两种方法在大多数情况下是相同的,但不是所有情况。采用以下构造函数:

public City(String name){
    this.name = name;
}

There is a namefield, and a namein local scope. Without this, the one in the local scope is implicitly used (right side of assignment) but with thiswe specifically refer to the field (left side of assignment). Before the assignment shown in the example I give, this.namewould have the default value of null(assuming it was declared String name) while namewould be the constructor parameter's value.

有一个name字段,一个name在本地范围内。如果没有this,则隐式使用局部作用域中的一个(赋值的右侧),但this我们专门指的是该字段(赋值的左侧)。在我给出的示例中显示的赋值之前,this.name将具有默认值null(假设它已声明String name)而name将是构造函数参数的值。

This is called shadowingand discussed formally in the JLS.

这称为阴影,并在JLS 中正式讨论。

回答by dckuehn

In your example there is no difference. You're just choosing to be explict about the variable being a class variable. However if you did this:

在你的例子中没有区别。您只是选择明确说明变量是类变量。但是,如果您这样做:

public String getName(){
    String name = "David";
    return this.name;
}

It would mean a lot, because you're not returning the local variable named name, you're returning the class variable.

这将意味着很多,因为您没有返回名为 的局部变量name,而是返回了类变量。