{{ ... }}Java中的Double Brace初始化语法()是什么?
为什么大括号没有在Java中定义单独的局部作用域?我期待这是主要大括号语言(C,C++,Java,C#)的共同特征.
class LocalScopeTester
{
public static void main(String... args)
{
Dog mine = new Dog("fido");
if (mine.getName().equals("ace"))
{
Dog mine = new Dog("spot"); // error: duplicate local
}
else
{
Dog mine = new Dog("barkley"); // error: duplicate local
{
Dog mine = new Dog("boy"); // error: duplicate local
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我得到了这种情况我无法理解阴影.例如以下代码
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 didnt throw an error when i named
// the parameter same as the instance …Run Code Online (Sandbox Code Playgroud)