JavaSE(05) -方法

发布时间 2023-09-15 14:44:22作者: 大宝贝94106

JavaSE(05) -方法

p63 什么是方法

  1. 什么是方法 : 方法是程序当中最小的执行单元.
  2. 应用场景 : 重复的代码, 具有独立功能的代码可以抽取的方法中.
  3. 他的好处 : 提高代码的复用性和可维护性.

p64 简单的方法定义和调用

方法的命名规则 : 见名之意, 驼峰命名.

方法的运行 : 看到方法进入方法 -> 执行完毕回到调用处.

p65 带参数的方法

***注意点 : 方法调用时, 参数的数量和类型必须(实参)与方法定义中小括号里面的变量(形参)一一对应, 否则程序将报错. ***

形参和实参

形参 : 全称形式参数 , 是指方法定义中的参数.

实参 : 全称实际参数 , 方法调用中的参数.

方法定义技巧

  1. 我要干什么? -> 方法体
  2. 我干这件事需要什么? -> 参数
  3. 方法的调用处是否需要继续使用方法的结果 ->返回值

练习1

需求 : 定义一个方法, 求长方形的周长, 在方法中输出结果.

 public static void main(String[] args) {
        getLength(1.2,2);
    }
	//计算长方形周长的方法
    public static void getLength(double len,double width){
        double result = (len+width)*2;//计算周长
        System.out.println(result);
    }

练习2

需求 : 定义一个方法, 求圆的面积, 在方法中输出结果.

public static void main(String[] args) {
        getCircleArea(1);
    }
//计算圆的面积方法
    public static void getCircleArea(double radius){
        double result = Math.PI * radius * radius;
        System.out.println(result);
    }

p66 带返回值的方法

方法的返回值会返回给方法的调用处.

  1. 什么时候用到有返回值的方法? -> 在调用处根据方法的结果,去编写另外一段代码时.也就是方法的调用处是否需要继续使用方法的结果.
  2. 有返回值方法的调用格式 : -> 直接调用, 赋值调用, 输出调用

练习. 比较大小

需求 : 定义两个方法, 比较两个长方形的面积.哪个更大?

public class Method {
    public static void main(String[] args) {
        double area1 = getArea(12, 8.5);//调用getArea方法得到面积
        double area2 = getArea(10, 10.5);//返回值赋值给变量
        compare(area1,area2);//调用compare方法,打印比较结果
    }
    //计算长方形面积的方法
    public static double getArea(double len, double width) {
        double Area = len * width;
        return Area;
    }
    //两个长方形比较的方法
    public static void compare(double area1,double area2){
        if (area1 > area2){
            System.out.println("第一个更大");
        }else if (area2 > area1){
            System.out.println("第二个更大");
        }else{
            System.out.println("两个一样大");
        }
    }
}

p67 方法小结

  1. 方法不调用就不执行.

  2. 方法与方法之间是平级关系, 不能互相嵌套定义.

  3. 方法的编写顺序和执行顺序无关.

  4. 方法的返回值为void, 表示没有返回值,没有返回值的方法可以省略return语句不写.如果要写return, 后面不能跟具体数据.

  5. return下面, 不能编写代码, 因为永远执行不到, 属于无效代码.

return关键字

  • 方法没有返回值 : 可以省略不写, 如果书写, 表示方法结束.
  • 方法有返回值 : 必须要写, 表示方法结束和返回结果.

p68 方法重载

简单的记忆 : 同一个类中, 方法名相同, 参数不同的方法. 与返回值无关.

参数不同 : 个数不同, 类型不同, 顺序不同.

注意 : 顺序不同可以构成重载, 但是不建议这样!

练习 方法重载

需求 : 使用方法重载思想, 设计比较两个整数是否相同的方法.
要求兼容全部整数类型(byte,short,int,long)

小技巧 : 没有用到的变量或方法 , 在IDEA中都是灰色的 , 所以就能看到用到了哪个方法.

/*
练习27 : 需求 : 使用方法重载思想, 设计比较两个整数是否相同的方法.
要求兼容全部整数类型(byte,short,int,long)
 */
public class Method_Reload_compare {
    public static void main(String[] args) {
       byte a = 1;
       byte b = 1;
       compare(a,b);//byte
        compare((short) 5,(short) 4);//short
       compare(9998999899l,9998999898l);//long
        compare(123,123);//int
    }
    //较两个整数是否相同的方法- byte
    public static void compare(byte b1,byte b2){
        System.out.print("byte :");
        System.out.println(b1==b2);
    }
    //short
    public static void compare(short s1,short s2){
        System.out.print("short :");
        System.out.println(s1==s2);
    }
    //int
    public static void compare(int i1,int i2){
        System.out.print("int :");
        System.out.println(i1==i2);
    }
    //long
    public static void compare(long L1,long L2){
        System.out.print("long :");
        System.out.println(L1==L2);
    }
}

p69 方法的三个练习

练习1 : 数组的遍历

需求 : 设计一个方法用于数组遍历, 要求遍历的结果是在一行上的. 例如: [11, 22, 33, 44]

练习2 : 数组最大值

需求 : 设计一个方法求数组的最大值, 并返回最大值.

练习3 : 判断是否存在

需求 : 设计一个方法判断数组中的某一个数是否存在, 将结果返回给调用处.

/*
 * 需求 : 设计一个方法用于数组遍历, 要求遍历的结果是在一行上的. 例如: [11, 22, 33, 44]
 * 需求 : 设计一个方法求数组的最大值, 并返回最大值.
 * 需求 : 设计一个方法判断数组中的某一个数是否存在, 将结果返回给调用处.
 * */
public class exersise_3 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 13, 5, 11, 7, 8};
        printArray(arr);
        int max = getMax(arr);
        System.out.println(max);
        boolean exist = contains(arr, 0);
        System.out.println(exist);
    }

    //遍历数组的方法
    public static void printArray(int[] array) {
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            if (i == array.length - 1) {
                System.out.println(array[i] + "]");
            } else
                System.out.print(array[i] + ", ");
        }
    }

    //求数组的最大值的方法
    public static int getMax(int[] array) {
        int max = array[0];
        //从1开始提高效率
        for (int i = 1; i < array.length; i++) {
            if (max < array[i]) {
                max = array[i];
            }
        }
        return max;
    }

    //判断数字是否存在的方法
    public static boolean contains(int[] array, int number) {
        for (int i = 0; i < array.length; i++) {
            if (number == array[i]) {    //需要判断的数和数组元素比较
                return true;
            }
        }
        return false;
    }
}

return & break 关键字的区别 :

return : 和循环没什么关系 , 跟方法有关 , 表示: 1结束方法 2返回结果 , 如果方法执行到了return , 那么整个方法全部结束, 里面的循环也随之结束了.

break : 跟方法没有关系 , 它是用来结束循环或者switch的.

p70 练习-拷贝数组

需求 : 定义一个方法copyOfRange(int [] arr, int from , int to)
功能 : 将数组arr中从索引from(包含from)开始.到索引to结束(不包含to)的元素复制到新数组中,将新数组返回.

/*
* 需求 : 定义一个方法copyOfRange(int [] arr, int from , int to)
功能 : 将数组arr中从索引from(包含from)开始.到索引to结束(不包含to)的元素复制到新数组中,将新数组返回.
* */
public class CopyOfRange {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] newArray = copyOfRange(arr, 3, 7);
        printArray(newArray);
    }

    //截取数组方法
    public static int[] copyOfRange(int[] arr, int from, int to) {
        int[] newArr = new int[to - from];//新数组长度为to-from
        int index = 0;  //伪造索引思想, 在没有任何一个变量能表示我索引变化范围的时候使用
        for (int i = from; i < to; i++) {//只遍历原数组需要的部分
//            System.out.println(arr[i]);
            newArr[index] = arr[i];//newArr索引从0开始
            index++;    //每次递增
        }

        return newArr;
    }

    //遍历数组方法
    public static void printArray(int[] array) {
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            if (i == array.length - 1) {
                System.out.print(array[i] + "]");
            } else {
                System.out.print(array[i] + ", ");

            }
        }
    }
}

p71 方法的基本内存原理

  • 栈 : 方法运行时使用的内存 , 方法进栈运行 , 运行完毕就出栈.
  • 堆 : new出来的 , 都在堆内存中开辟一个空间.

在栈内存中, 方法运行的规律是先进后出.

p72 基本/引用数据类型 p73 方法的值传递

  • 基本数据类型 : 数据值存在自己的空间中.

​ 特点 : 赋值给其它变量, 也是赋的真实的值.

  • 引用数据类型 : 数据值是存储在其它空间中, 自己空间中存储的是地址值.

​ 特点 : 赋值给其它变量 , 赋的也是地址值.