由於在C++裏邊是支持的,自己寫了發現報錯了。查了一下:
局部變量for(String s:l)不允許,因爲上面的定義過s——String[] s;
雖然數據類型不同,但是java就是不允許,編譯器會報告變量x已經定義過。
java設計者認爲這樣做會導致程序混亂。
但是全局變量和局部變量同名是允許的。
public class testnametype {
static int b =3;
int a;
{
a=1;
}
public static void main(String... s){
System.out.println(new testnametype().a+" "+testnametype.b);
int a=2;
int b=4;
System.out.println(a+" "+b);
}
}