0
点赞
收藏
分享

微信扫一扫

【黑马程序员 Java 笔记】方法

小安子啊 2022-02-15 阅读 54
java

目录

1、方法概述

2、简单方法的定义和调用

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

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

5、方法重载

6、方法的参数传递


1、方法概述

        方法是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集。

                方法必须先创建才可以使用,该过程称为方法定义。(方法不能嵌套定义)

                方法创建后并不是直接运行的,需要手动使用后才能执行,该过程称为方法调用。(方法必须先定义后调用,否则程序将报错)

         方法定义通用格式:

                public static 返回值类型 方法名(参数){

                        方法体;

                        return 数据;

                }

2、简单方法的定义和调用

        定义:

public static void 方法名(){   // void 表示无返回值,可以省略 return;也可以单独地书写 return,后面不加数据
    // 方法体
}

        调用: 方法名();

public class Method_2 {
    public static void main(String[] args) {
        // 调用方法
        isEvenNumber(); // true

        // 4. 在 main方法中调用定义好的方法
        getMax();   // 20
    }

    // 需求:定义一个方法,在方法中定义一个变量,判断该数据是否是偶数
    public static void isEvenNumber(){
        // 定义变量
        int number = 10;

        // 判断该数据是否是偶数
        if (number % 2 == 0){
            System.out.println(true);
        }
        else
            System.out.println(false);
    }

    /*
   需求:设计一个方法用于打印两个数中的较大数

   思路:
       1. 定义一个方法,用于打印两个数中的较大数,如 getMax()
       2. 方法中定义两个变量,用于保存两个数字
       3. 使用分支语句分两种情况对两个数字的大小关系进行处理
       4. 在 main方法中调用定义好的方法
   */
    // 1. 定义一个方法,用于打印两个数中的较大数,如 getMax()
    public static void getMax(){
        // 2. 方法中定义两个变量,用于保存两个数字
        int a = 10;
        int b = 20;

        // 3. 使用分支语句分两种情况对两个数字的大小关系进行处理
        if (a > b){
            System.out.println(a);
        }
        else
            System.out.println(b);
    }
}

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

                形参:方法定义中的参数 —— 等同于变量定义格式,如:int number

                实参:方法调用中的参数 —— 等同于使用变量或常量,如:10 number

        定义: public static void 方法名(数据类型 变量名){……}

                         public static void isEvenNumber(int number){……}

                         public static void isEvenNumber(int number1, int number2){……}

                方法定义时,参数中的数据类型与变量名都不能缺少,缺少任意一个程序将报错;多个参数之间使用逗号分隔。

        调用: 方法名(变量名/常量值);

                        isEvenNumber(5);

                        getMax(1, 2);

                方法调用时,参数的数量与类型必须与方法定义中的设置相匹配,否则程序将报错。

public class Method_3 {
    public static void main(String[] args) {
        // 常量值的调用
        isEvenNumber(5);    // false
        // 变量的调用
        int number = 10;
        isEvenNumber(number);   // true

        getMax(20,30);  // 30
    }

    // 需求:定义一个方法,该方法接收一个参数,判断该数据是否是偶数
    public static void isEvenNumber(int number){
        if (number % 2 == 0){
            System.out.println(true);
        }
        else {
            System.out.println(false);
        }
    }

    /*
        需求:设计一个方法用于打印两个数中的较大数,数据来自于方法参数

        思路:
            1. 定义一个方法,用于打印两个数中的较大数,如 getMax()
            2. 为方法定义两个参数,用于接收两个数字
            3. 使用分支语句分两种情况对两个数字的大小关系进行处理
            4. 在 main方法中调用定义好的方法
    */
    public static void getMax(int a, int b){
        if (a > b){
            System.out.println(a);
        }
        else
            System.out.println(b);
    }
}

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

        定义:

public static 数据类型 方法名(参数){
    ……
    return 数据;
}

public static boolean isEvenNumber(int number){
    ……
    return true;
}

                方法定义时,return 后面的返回值与方法定义上的数据类型要匹配,否则程序将报错。

        调用: 方法名(参数);

                        isEvenNumber(5);

                   数据类型 变量名 = 方法名(参数);

                        boolean flag = isEvenNumber(5);

                方法的返回值通常会使用变量接收,否则该返回值将无意义。

public class Method_4 {
    public static void main(String[] args) {
        boolean flag = _isEvenNumber_(11);
        System.out.println(flag);   // false

        System.out.println(_getMax_(30,40));    // 40
    }

    // 需求:定义一个方法,该方法接收一个参数,判断该数据是否是偶数,并返回真假值
    public static boolean _isEvenNumber_(int number){
        if (number % 2 == 0){
            return true;
        }
        else {
            return false;
        }
    }

    public static int _getMax_(int a, int b){
        if (a > b){
            return a;
        }
        else {
            return b;
        }
    }
}

5、方法重载

        同一个类中定义的多个方法,满足以下条件相互构成重载:

                 1)多个方法在同一个类中

                 2)多个方法具有相同的方法名

                 3)多个方法的参数不相同——类型不同或数量不同

        特点:

                 重载仅对应方法的定义,与方法的调用无关,调用方式参照标准格式。

                 重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关。换句话说,不能通过返回值来判定两个方法是否相互构成重载。

public class Method_5 {
    public static void main(String[] args) {
        System.out.println(sum(1,2));
        System.out.println(sum(1.0,2.0));
        System.out.println(sum(1,2,3));

        System.out.println("\n");

        System.out.println(compare(10,20));
        System.out.println(compare((byte)10,(byte)20));
        System.out.println(compare((short)10,(short)20));
        System.out.println(compare(10L,20L));
    }

    // 需求:简单求和
    public static int sum(int a, int b){
        return a + b;
    }
    public static double sum(double a, double b){
        return a + b;
    }
    public static int sum(int a, int b, int c){
        return a + b + c;
    }

    /*
        需求:使用方法重载的思想,设计比较两个整数是否相等的方法,兼容全整数类型(byte,short,int,long)
    */
    public static boolean compare(int a, int b){
        System.out.println("int");
        return a == b;
    }
    public static boolean compare(byte a, byte b){
        System.out.println("byte");
        return a == b;
    }
    public static boolean compare(short a, short b){
        System.out.println("short");
        return a == b;
    }
    public static boolean compare(long a, long b){
        System.out.println("long");
        return a == b;
    }
}

// 运行结果
/*
3
3.0
6


int
false
byte
false
short
false
long
false
*/

6、方法的参数传递

        对于基本数据类型的参数,形参的改变不影响实参的值

/*
对于基本数据类型的参数,形参的改变不影响实参的值
*/
public class ArgsDemo01 {
    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;
    }
}

// 运行结果
/*
调用change方法前:100
调用change方法后:100
*/

        对于引用数据类型的参数,形参的改变会影响实参的值

/*
对于引用数据类型的参数,形参的改变会影响实参的值
*/
public class ArgsDemo02 {
    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;
    }
}

// 运行结果
/*
调用change方法前:20
调用change方法后:200
*/

public class Method_6 {
    public static void main(String[] args) {
        int[] arr = {11, 22, 33, 44, 55};
        printArray(arr);
        System.out.println(getMax(arr));
    }

    /*
        数组遍历
        需求:设计一个方法用于遍历数组,要求遍历的结果是在一行上的。如 [11, 22, 33, 44, 55]
    */
    public static void printArray(int[] arr){
        System.out.print("[");
        for(int i = 0; i < arr.length; i++){
            if (i == arr.length - 1){
                System.out.print(arr[i]);
            }
            else {
                System.out.print(arr[i] + ", ");
            }
        }
        System.out.println("]");
    }

    /*
        数组最大值
        需求:设计一个方法用于获取数组中元素的最大值,调用方法并输出结果
    */
    public static int getMax(int[] arr){
        int max = arr[0];
        for(int j = 1; j < arr.length; j++){
            if(arr[j] > max){
                max = arr[j];
            }
        }
        return max;
    }
}
举报

相关推荐

0 条评论