1
我得到這個錯誤:爲什麼常規沒有找到我的變量在其範圍內
Apparent variable 'b' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'b' but left out brackets in a place not allowed by the grammar.
@ line 11, column 12.
int a = (b + 5);
^
爲什麼沒有承認B中的變量?我正在嘗試測試Groovy中的範圍工作方式。它是靜態的還是動態的?
class practice{
static void main(String[] args)
{ int b=5;
foo(); // returns 10
bar(); // returns 10
println('Hello World');
}
//public int b = 5;
static void foo()
{
int a = (b + 5);
println(a);
}
static void bar()
{
int b = 2;
println(foo());
}
}