类的字段和方法的参数会干扰吗?

发布于 2024-08-25 11:00:01 字数 343 浏览 6 评论 0原文

我有一个带有名为“a”的字段的类。在类中我有一个方法,在该方法的参数列表中我也有“a”。那么,我会在方法内部看到哪个“a”?它是字段还是方法的参数?

public class myClass {
   private String a;
   // Method which sets the value of the field "a".
   public void setA(String a) {
     a = a;
   }
}

顺便说一下,还有类似的情况。方法有一些局部(方法)变量,其名称与字段名称一致。如果我在方法内引用这样的方法局部变量(字段或局部变量),那么该方法会“看到”什么?

I have a class with a fields called "a". In the class I have a method and in the list of arguments of this method I also have "a". So, which "a" I will see inside of the method? Will it be the field or it will be the argument of the method?

public class myClass {
   private String a;
   // Method which sets the value of the field "a".
   public void setA(String a) {
     a = a;
   }
}

By the way, there is a similar situation. A method has some local (for method) variables whose names coincide with the names of the fields. What will the "see" the method if I refer to such a method-local variable inside the method (the field or the local variable)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

禾厶谷欠 2024-09-01 11:00:01

更本地的范围具有优先权,因此参数a将隐藏字段a。实际上,您将参数a 的值设置为其自身。避免名称冲突(并提高可读性)的正确习惯用法是使用 this 显式标记类成员:

public void setA(String a) {
  this.a = a;
}

局部变量与成员变量也是如此:局部变量隐藏同名的成员变量。

The more local scope has the priority, so the parameter a will hide the field a. In effect, you set the value of parameter a to itself. The proper idiom to avoid name clashes (and improve readability) is to use this to explicitly mark the class member:

public void setA(String a) {
  this.a = a;
}

The same is true for local variables vs member variables: local variables hide member variables with the same name.

掩耳倾听 2024-09-01 11:00:01

要添加到所有建议的答案中:

public void setA(String a) {
   this.a = a;
}

重要的是要认识到省略 this 只会将参数设置为其自身。通过使用final

public void setA(final String a) {
   this.a = a;
}

您可以消除由于省略this而导致的错误。当指定不需要更改的参数和字段时,使用 final 是一个很好的做法。

To add to all the answers recommending:

public void setA(String a) {
   this.a = a;
}

it's important to realise that omitting the this will simply set the parameter to itself. By using final thus

public void setA(final String a) {
   this.a = a;
}

you can eliminate errors caused by omitting this. Using final is a good practise whenever specifying parameters and fields that aren't intentionally required to change.

盗琴音 2024-09-01 11:00:01

最接近的一个。也就是说,

 a = a;

在方法内部没有任何作用,因为两者都引用参数 a。要引用实例变量 a,可以使用 this 关键字。

 this.a = a;

The closest one. That is,

 a = a;

inside the method has no effect since both refer to the argument a. To refer to the instance variable a you use the this keyword.

 this.a = a;
弱骨蛰伏 2024-09-01 11:00:01

本地版本将使用相同的名称“隐藏”实例变量。在像您这样的访问器中解决此问题的一种模式是:

public void setA(String a) {
   this.a = a;
}

它使用 this 关键字来明确范围。

The local version will "shadow" the instance variable by the same name. One pattern to get around this in accessors like your is this:

public void setA(String a) {
   this.a = a;
}

which uses the this keyword to be explicit about scope.

燃情 2024-09-01 11:00:01

您需要使用 this 来访问 class 变量,否则它将始终采用参数变量。

You need to use this to access the class variable, otherwise it will always take the parameter variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文