java语言之方法的使用与递归算法

1. 什么是方法(函数)

  java语言的方法类似于其他语言的函数,是一段来完成特定功能的代码片段,       

2. 掌握方法的声明

    声明格式:修饰符  返回值类型 方法名 (参数列表){

    程序代码;

   return 返回值就;

    }

        方法中的参数:

                形式参数:在方法被调用时用于接收外界输入的数据。

                实际参数:调用方法时实际传给方法的数据。

            方法中的返回值(返回值类型:方法要返回的结果的数据类型,如一个方法没有返回值,必须给出返回值类型 void)

           (返回值:方法在执行完毕后返还给调用者的数据。)

           (return语句终止方法的运行并指定要返回的数据。)

3. 掌握方法的调用

      java语言中使用下述形式调用方法:

          对象变量名.方法名(实参列表);

      实参的数目,数据类型和次序必须和所调用方法声明的形参列表匹配。

      代码如下:

  

                                            public class Demo11{

                                                    public static void printInfor (){

                                               for (int i = 0; i<10 ; i++ ){
                          System.out.println(i);
                          }

                        }
                      public static int getMax(int i , int j){

                            int max=0;
                            if (i>j){
                            max=i;
                            }
                            else {
                              max=j;
                              }
                          return max;

                          }

                  public static void main (String [] args){

                        //System.out.println("第一次打印 ");
                        //printInfor();
                        //System.out.println("第二次打印 ");
                        //printInfor();
                         System.out.println("比较两个数输出较大的数 :");
                          int res = getMax(2,4);
                          System.out.println(res);


                      }


                     }

    

4. 掌握什么是方法的重载

    方法的重载指的是一个类中可以定义有相同名字,但参数列表不同的多个方法,调用时,会根据不同的参数列表选择对应的方法;

    参数列表是指参数的类型,个数,或顺序。

                    满足点认为是方法的重载:

                   1. 发生在同一个类中

                   2. 方法名相同

                   3. 参数列表不同(类型,大小,数量) 

      代码如下:

                              public class Demo11{
               public static int getMax(int i , int j){

                    int max=0;
                      if (i>j){
                        max=i;
            }
                      else {
                          max=j;
                      }
                        return max;

                          }
                public static int getMax(int i , int j , int k){
                              int max=(i>j) ? i : j;
                              if(max>k){
                                return max;
                              }
                                  else{
                                return k;
                                }

                              }

                    public static void main (String [] args){
                        System.out.println("比较两个数输出较大的数 :");
                          int res1 = getMax(2,4);
                            System.out.println(res1);
                                System.out.println("比较三个数输出较大的树 :");
                                    int res2 = getMax(3,5,4);
                                                System.out.println(res2);

                  }


                }     

5. 了解递归算法

      1. 递归调用指的是方法执行过程中出现该方法本身的调用。

      2. 递归算法关键要抓住的是:

          递归出口

          递推步向出口逼近

             代码如下:

  

                       public class Demo12
                          {
                              public static int multip1(int n )//一般求阶乘的方法。
                                {
                                      int res =1;
                                      for (int i=1 ;i<=n ; i++ )
                                           {
                                               res*=i;
                                            }
                                        return res;
                                  }
                                public static int multip2(int n)
                                    {
                                       if (n==1 || n==0)
                                           {
                                              return 1;
                                            }
                                         return n*multip2(n-1);
                                     }
                        public static void main(String [] args)
                             {
                                  System.out.println(multip1(3));
                                  System.out.println(multip2(7));
                              }
                         }

      

  

原文地址:https://www.cnblogs.com/wuW00/p/4583659.html