java中的方法(method)到底怎么用?给个例子

来源:互联网 发布:晨曦软件支持什么系统 编辑:程序博客网 时间:2024/06/05 23:52
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
Available to all methods in the class(类中所有的方法都可以用)
2)Block(块) scope
Available only with the block it is declared to,(只在他声明的块中有效) orwithin nested blocks(嵌套的块儿中)
3)Method(方法) scope
Available to a method which the variable is declaredwithin,(只在他声明的方法中有效)

以下例子中,i就是类变量,k 就是块儿变量,j就是方法变量,
public class VariableScope{
          static int i = 10;
          public static void main(String[] args){
                      int j = 20;

                      int r = cube(i);
。。。。。。由于篇幅限制,更多详情请见:http://www.mark-to-win.com/JavaBeginner/JavaBeginner1_web.html#Method
0 0
原创粉丝点击