zoukankan      html  css  js  c++  java
  • java方法

    1.4 java方法

    1.4.1方法的基本概念

    • 对功能进行封装

    • 一般一个功能一个方法(原子性)利用后期的扩展

    • java语句的集合

    • 包含于类或对象中

    1.4.2方法的定义和调用

    修饰符  返回值类型  方法名  (参数类型   参数名){
       方法体
       return 返回值;
       
           
           
    /*
     参数:
     形参:方法括号里的参数
     实参:主函数里传递的参数

    */

     

    1.4.3方法的重载

    1. 概念和规则

    public class Demo01 {
       public static void main(String[] args) {

           System.out.println(add(1,2));
           System.out.println(add(1,2,3));
           System.out.println(  add(1.1,1.2));
      }

       public static int add(int a , int b){
           return a+b;
      }
       public static double add(double a , double b){
           return a+b;
      }
       public static int add(int a , int b , int c){
           return a+b+c;
      }
    }

     

    1.4.4可变参数

    1. 概念

      同类型的可变参数传递给方法

    2. 规则

      一个方法里只能传一个可变参数,且必须是方法的最后一个参数,所有参数必须在其之前声明

    3. 代码

    public static void printMax(double...numbers){
       if(numbers.length==0){
           System.out.println("No argument passed");
           return ;
          }
           double result = numbers[0];
           //排序
           for(int i=1; i < number.length; i++){
               if(numbers[i] > result){
                   result = numbers[i];
              }
      }
       System.out.println("The max value is"+result);
    }

    1.4.5递归

    1. 概念

      • 方法调动自身

      • 通常把大问题化成相似的小问题来求解

    2. 递归的结构

      • 递归头

        即递归的出口

      • 递归体

        调用自身的代码

    3. 代码

      public class Demo02 {
         public static void main(String[] args) {
             System.out.println(com(5));
        }
         public static int com(int x){
             if(x==1){
                 return 1;
            }else{
                 return x*(x-1);
            }
        }
      }
    4.  

  • 相关阅读:
    linux shell 获取文件夹全文绝对路径
    Selenium自动化获取WebSocket信息
    Automatically generating nice graphs at end of your Load Test with Apache JMeter and JMeter-Plugins
    卡夫卡与风暴:卡夫卡和风暴的明智比较
    Apache Kafka安全| Kafka的需求和组成部分
    Kafka Schema Registry | 学习Avro Schema
    QT 内部使用函数列表
    QT图标添加
    QT快捷键
    QT软件上架以及自启动
  • 原文地址:https://www.cnblogs.com/cjdstudy/p/12322340.html
Copyright © 2011-2022 走看看