为什么不能在循环中隐藏局部变量?
我遇到了这种情况,我无法理解阴影。例如下面的代码:
class Foo {
int a = 5;
void goFoo(int a) {
// No problem naming parameter as same as instance variable
for (int a = 0; a < 5; a++) { }
// Now the compiler complains about the variable a on the for loop
// I thought that the loop block had its own scope so I could shadow
// the parameter, why the compiler didn't throw an error when i named
// the parameter same as the instance variable?
}
}
I got this situation I can't understand about shadowing. For example the following code:
class Foo {
int a = 5;
void goFoo(int a) {
// No problem naming parameter as same as instance variable
for (int a = 0; a < 5; a++) { }
// Now the compiler complains about the variable a on the for loop
// I thought that the loop block had its own scope so I could shadow
// the parameter, why the compiler didn't throw an error when i named
// the parameter same as the instance variable?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 Java 中(与 C++ 不同),当另一个同名局部变量“在作用域内”时,您无法声明局部变量。
在 Java 中你不能这样做,
但是 C++ 允许你这样做
In Java (unlike, say, in c++) you cannot declare a local variable when another local variable with the same name is "in scope".
You cannot do this in Java
However c++ allows you to do this
您可以使局部变量遮蔽实例/静态变量 - 但不能使一个局部变量(您的循环计数器)遮蔽另一个局部变量或参数(您的参数)。
来自 Java 语言规范,第 14.4.3 节< /a>:
请注意“字段名称”部分 - 它指定它必须是一个被隐藏的字段。
以及第 8.4.1 节:
(它继续讨论本地类和匿名类,但它们与您的情况无关。)
You can make a local variable shadow an instance/static variable - but you can't make one local variable (your loop counter) shadow another local variable or parameter (your parameter).
From the Java Language Specification, section 14.4.3:
Note the "field name" part - it's specifying that it has to be a field that is shadowed.
And from section 8.4.1:
(It goes on to talk about local classes and anonymous classes, but they're irrelevant in your case.)
它类似于
在同一范围内多次声明
a
,这是不可接受的。或者只是类似于
另请参阅
it is similar to
so multiple declaration of
a
on the same scope, it is not acceptable.or simply it is similar to
Also See
变量的范围也取决于块的层次结构。
即如果你像这样使用
,即如果在外部块中声明了一个变量,那么你不能在内部块中声明相同的变量。你可以用另一种方式做到这一点。
The scope of the variable depends on the hierarchy of the block as well.
ie if u use like this
That is if a variable is declared in the outer block then you can not declare the same in the block which is inner. the other way you can do it.
但是,正如代码所示,您没有在新范围中声明第二个“a”。它位于 goFoo() 块本身的范围内。
But you don't declare the second "a" in that new scope, as your code shows. It's in the scope of the goFoo() block itself.
问题不在于循环遮盖了类字段,而该名称已被参数使用。
两种选择:一是更改循环:
这使用参数作为索引变量。不清楚为什么你会有一个参数,但都是一样的......
另一个选项是将循环变量或参数重命名为其他名称。
The problem isn't that the loop is shadowing the class field, the name is already used by the parameter.
Two options: One is to change the loop:
This uses the parameter as the index variable. Not clear why you would have a parameter, but all the same...
The other option is to rename the loop variable or the parameter to something else.
这不是阴影,而是这里的冲突。两个
a
都在方法范围内。不能在同一范围内定义两个同名的变量。It is not shadowing, it is a conflict here. Both
a
are in the method scope. One cannot define two variables of same name in the same scope.