JAVA 中 static 的理解

来源:互联网 发布:微信改单是什么软件 编辑:程序博客网 时间:2024/05/15 23:46

在主类中,main 方法具有 static ,也就要求此方法中只能调用具有 static 特性的其余方法(此主类中的)。
所以在完成递归时,其方法需要定义在新的类中

一个求 5! 的实现

package com.leon;public class Bouns {    public static void  main(String[] args) {        int n = 5;        Rec fRec = new Rec();        System.out.println(n+"!  = " + fRec.rec(n));    }}class Rec{    public long rec(int n) {        long value = 0;        if (n==1) {            value = 1;        }else {            value = n * rec(n-1);        }        return value;    }}
0 0
原创粉丝点击