参数范围与局部变量范围? [英] Parameter scope vs local variable scope?
本文介绍了参数范围与局部变量范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在阅读我的AP cs书,它谈到了三种类型的变量:
I was reading my AP cs book and it talked about three types of variables:
•实例变量
•局部变量
•参数
实例变量在整个班级等中都可见...
参数仅在方法中可用,因此是局部变量。 。 。
Instance variables are visible throughout the class etc... Parameters are only usable within the method and so are local variables . . .
因此我的问题是,如果它们包含相同的范围,他们为什么会将参数和局部变量分类为不同类别的变量。 。 。尽管它们的用途不同。
Therefore my question is why would they classify Parameters and Local variables as different categories of variables if they contain the same scope. . . Despite the different uses of them.
推荐答案
因为它们不一定具有相同的范围。
Because they don't necessarily have the same scope.
这种情况:
// this is garbage code
public void doSomething(int foo) {
int meh = 0;
while (true) {
// can access foo and meh
int blah = meh++;
if (blah == foo) {
break;
}
}
// won't compile, can't access blah anymore
System.out.println(blah);
// will compile
System.out.println(foo);
// will compile as well
System.out.println(meh);
}
这篇关于参数范围与局部变量范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文