第一章_Java基础语法(7)_方法

一、方法的概念

  • 方法(method)是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集
  • 注意:
    • 方法必须先创建才可以使用,该过程称为方法定义
    • 方法创建后并不是直接可以运行的,需要手动使用后,才执行,该过程称为方法调用

二、方法的定义和调用

  • 无参数方法定义和调用
    • 定义格式: public static void 方法名 ( ) { // 方法体; }
    • 调用格式:方法名();
    • 注意:方法必须先定义,后调用,否则程序将报错
public class Demo1Method {
    public static void main(String[] args) {
        eat();
    }
    public static void eat (){
        study();
        System.out.println("吃饭");
    }
    public static void study(){
        System.out.println("学习");
    }
}
  • 方法的调用过程
    • 程序开始运行,.class字节码文件会进入方法区,其中包含了所定义的方法,包含主方法main
    • 每个方法在被调用执行的时候,都会进入栈内存,并且拥有自己独立的内存空间,方法内部代码调用
      完毕之后,会从栈内存中弹栈消失(FILO,first in last out,先进后出)

在这里插入图片描述

  • 案例:使用方法判奇偶数
public class Demo2Method {
    public static void main(String[] args) {
        // 3. main方法中调用method
        method();
    }
    // 1. 定义method方法
    public static void method(){
        //  2. 方法中定义变量, 使用if语句判断是奇数还是偶数
        int num = 11;
        if(num % 2 == 0){
            System.out.println("偶数");
        }else{
            System.out.println("奇数");
        }
    }
}

三、带参数方法的定义和调用

  • 定义格式:
    • public static void 方法名 (参数1) {方法体;}
    • public static void 方法名 (参数1, 参数2, 参数3...) {方法体; }
  • 调用格式
    • 方法名(参数);
    • 方法名(参数1,参数2。。。);
  • 注意事项
    • 方法定义时,参数中的数据类型与变量名都不能缺少,缺少任意一个程序将报错
    • 方法定义时,多个参数之间使用逗号( ,)分隔
    • 方法调用时,参数的数量与类型必须与方法定义中的设置相匹配,否则程序将报错.
public class Demo1Method {
    public static void main(String[] args) {
        isEvenNumber(10);
    }
    public static void isEvenNumber(int num){
        if(num % 2 == 0){
            System.out.println("偶数");
        }else{
            System.out.println("奇数");
        }
    }
}
  • 形参和实参
    • 方法定义中的参数,等同于变量定义格式,例如:int number
    • 实参:方法调用中的参数,等同于使用变量或常量,例如: 10 number
  • 带参数方法案例:打印n-m之间所有的奇数
public class Demo2Method {
    /*
        需求:设计一个方法(print) 用于打印 n 到 m 之间所有的奇数
     */
    public static void main(String[] args) {
        // 5:main方法中调用print方法,传入两个实际参数
        print(20,10);
    }

    // 1:定义方法,名称为print
    // 2:为方法添加两个int类型的形参,准备接受调用者传递过来的实参
    public static void print(int n, int m){
        System.out.println(n + "到" + m + "之间的奇数为:");
        // 3:方法中设计for循环,循环从n开始,到m结束
        for(int i = 20; i <= 10; i++){
            // 4:循环中加入if判断,是奇数,则打印
            if(i % 2 == 1){
                System.out.println(i);
            }
        }
    }
}

四、带返回值方法的定义和调用

  • 定义格式
    • 方法定义时return后面的返回值与方法定义上的数据类型要匹配,否则程序将报错
  public static 数据类型 方法名 ( 参数 ) { 
  	return 数据 ;
  }
  • 调用格式
    • 方法的返回值通常会使用变量接收,否则该返回值将无意义
  方法名 ( 参数 ) ;
  数据类型 变量名 = 方法名 ( 参数 ) ;
  • 带返回值方法的案例:求两个数的最大值
public class Demo2Method {
    /*
        需求:设计一个方法可以获取两个数的较大值,数据来自于参数
     */
    public static void main(String[] args) {
        // 3. 在main()方法中调用定义好的方法并使用 【 变量保存 】
        System.out.println(getMax(10,20));  // 输出调用
        int result = getMax(10,20);
        System.out.println(result);
        for(int i = 1; i <= result; i++){
            System.out.println("HelloWorld");
        }
    }
    // 方法可以获取两个数的较大值
    public static int getMax(int a, int b){
        if(a > b){
            return a;
        }else{
            return b;
        }
    }
}

五、方法重载

  • 方法重载概念
    • 方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载
      • 多个方法在同一个类中
      • 多个方法具有相同的方法名
      • 多个方法的参数不相同,类型不同或者数量不同
    • 重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,换句话说不能通过返回值来判定两个方法是否相互构成重载
public class Demo1Overload {
    /*
    需求:使用方法重载的思想,设计比较两个整数是否相同的方法,兼容全整数类(byte,short,int,long)
     */
    public static void main(String[] args) {
        short a = 10;
        short b = 20;
        System.out.println(compare(a,b));
    }
    public static boolean compare (int a, int b){
        return a == b;
    }
    public static boolean compare (byte a, byte b){
        return a == b;
    }
    public static boolean compare (short a, short b){
        return a == b;
    }
    public static boolean compare (long a, long b){
        return a == b;
    }
}

六、方法的参数传递

  • 方法参数传递基本数据类型
    • 方法传递基本数据类型时,传递的是具体的值
public class Test1 {
    /*
         方法参数传递为基本数据类型 :传入方法中的, 是具体的数值.
     */
    public static void main(String[] args) {
        int number = 100;
        System.out.println("调用change方法前:" + number);
        change(number);
        System.out.println("调用change方法后:" + number);
    }
    public static void change(int number) {
        number = 200;
    }
}
  • 方法参数传递引用类型
    • 引用数据类型的传参,传入的是地址值,内存中会造成两个引用指向同一个内存的效果,所以即使方法弹栈,堆内存中的数据也已经是改变后的结果
public class Test2 {
    /*
         方法参数传递为引用数据类型 :传入方法中的, 是内存地址.
     */
    public static void main(String[] args) {
        int[] arr = {10, 20, 30};
        System.out.println("调用change方法前:" + arr[1]);
        change(arr);
        System.out.println("调用change方法后:" + arr[1]);
    }
    public static void change(int[] arr) {
        arr[1] = 200;
    }
}

七、方法的应用案例

  • 案例1:使用方法实现数组遍历
public class Test1 {
    /*
        需求:设计一个方法用于数组遍历,要求遍历的结果是在一行上的。例如:[11, 22, 33, 44, 55]
     */
    public static void main(String[] args) {
        // 1.定义一个数组,用静态初始化完成数组元素初始化
        int[] arr = {11, 22, 33, 44, 55};
        // 4.调用遍历方法
        printArray(arr);
        System.out.println("另外一段代码逻辑 ");
    }

    public static void printArray(int[] arr){
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if(i == arr.length -1){
                // 如果满足条件, 说明是最后一个元素, 最后一个元素, 特殊处理
                System.out.println(arr[i] + "]");
            }else{
                // 3.遍历打印的时候,数据不换行
                System.out.print(arr[i] + ", ");
            }
        }
    }
}
  • 案例2:使用方法获取数组中元素的最大值
public class Test2 {
    /*
        需求:设计一个方法用于获取数组中元素的最大值
     */
    public static void main(String[] args) {
        // 1.定义一个数组,用静态初始化完成数组元素初始化
        int[] arr = {11, 55, 22, 44, 33};
        // 3.调用获取最大值方法,用变量接收返回结果
        int max = getMax(arr);
        //  4.把结果输出在控制台
        System.out.println(max);
    }

    public static int getMax(int[] arr){
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(max < arr[i]){
                max = arr[i];
            }
        }
        return max;
    }
}
  • 案例3:使用方法同时获取数组最大值和最小值
public class Test3 {
    /*
        需求:设计一个方法,该方法能够同时获取数组的最大值,和最小值
        注意: return语句, 只能带回一个结果.
     */
    public static void main(String[] args) {
        int[] arr = {11,55,33,22,44};
        int[] maxAndMin = getMaxAndMin(arr);
        System.out.println(maxAndMin[0]);
        System.out.println(maxAndMin[1]);
    }

    public static int[] getMaxAndMin(int[] arr){
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(max < arr[i]){
                max = arr[i];
            }
        }
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(min > arr[i]){
                min = arr[i];
            }
        }
        int[] maxAndMin = {min, max};
        return maxAndMin;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无休止符

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值