java 方法

方法命名规范要求
  类的命名规范:“全部单词的 首字母必须大写”。那么在定义方法的时候也是有命名规范要求的:“第 一个单词的首字母小写,之后每个单词的首字母大写”,那么这就是方法 的命名规范。

递归调用——数字的累加操作:

  

 1 public class Test{
 2   public static int sum(int num){
 3         if(num==1){
 4              return 1;
 5         }else{
 6               return num+sum(num-1);
 7           }  
 8     }
 9      public static void  main(String[] args){
10          System.out.print(sum(100));
11      }
12 
13 }                

使用可变参数定义方法:

  

 1 public class Demo04 {
 2     public static void fun(int... arg){
 3         for(int i=0; i<arg.length;i++){
 4             System.out.print(arg[i]+" ");
 5         }
 6         System.out.println();
 7     }
 8     public static void main(String[] args) {
 9         fun();
10         fun(1);
11         fun(2,3);
12         fun(1,2,3,4,5);
13     }
14 
15 }

结果:

  

原文地址:https://www.cnblogs.com/dogLin/p/5862201.html