7.方法(method)
被调例子,
int add(int x, int y){
return x+y;
}
主调例子,
for example:
int result = add(5,3);
大家可以看出来和c语言是一样的。
7.1 Variable Scope(变量范围)
1)Class(类) scope
类中所有的方法都可以用
2)Block(块) scope
只在他声明的块中有效 or 嵌套的块儿中
3)Method(方法) scope
只在他声明的方法中有效
下例中,i是类变量,k 是块儿变量,j是方法变量,
public class Test{
static int i = 10;
public static void main(String[] args){
int j = 20;
int r = cube(i);
System.out.println(r);
r = cube(j);
System.out.println(r);
{
int k = 30;
r = cube(k);
System.out.println(r);
}
//r = cube(k);there is an error here.错误
}
static int cube(int n){
return n*n*n;
}
}
更多请看:https://blog.youkuaiyun.com/qq_44639795/article/details/103145051
本文深入探讨了Java编程语言中变量的作用域概念,包括类作用域、块作用域和方法作用域。通过实例分析,详细解释了不同作用域内的变量使用规则及常见错误。
1212

被折叠的 条评论
为什么被折叠?



