方法

一、方法的基本使用

1.1 方法的概念

方法指在类中定义一段特定功能的代码,封装以便重复使用,有利于代码的维护和扩展。

1.2 基本语法

定义语法:

public static void 方法名(){

}

注意:方法定义在类的内部,并且与其他方法并列。

调用:

在其他方法中,使用该方法名();方式调用。

1.3 简单定义和调用
  1. public class Demo2 {
  2. public static void main(String[] args) {
  3. System.out.println("床前明月光,");
  4. printLine();
  5. System.out.println("疑是地上霜。");
  6. printLine();
  7. System.out.println("举头望明月,");
  8. printLine();
  9. System.out.println("低头思故乡。");
  10. printLine();
  11. }
  12. // 方法定义,在类的内部,与其他方法并列
  13. public static void printLine() {
  14. for (int i = 0; i < 10; i++) {
  15. System.out.print("-");
  16. }
  17. System.out.println();
  18. }
  19. }

二、方法的参数

2.1 参数的概念

参数是指方法在定义时,写在方法名称后面的括号中的变量,通过该变量的值来确定方法执行的结果。

2.2 形参与实参

在方法定义时,方法中参数实际是没有任何值的,此时该参数作为形式上的参数(形参)存在。而在方法调用时,会将实际的值从参数传入,传入的值即为实际的参数(实参)。

public class Demo2 {
    public static void main(String[] args) {
        System.out.println("床前明月光,");
        int num = 10;
        printLine(num); // num为实参
        System.out.println("疑是地上霜。");
        printLine(5);
        System.out.println("举头望明月,");
        printLine(8);
        System.out.println("低头思故乡。");
        printLine(6);
    }

    // 方法定义,在类的内部,与其他方法并列
    public static void printLine(int count) { // count为形参
        for (int i = 0; i < count; i++) {
            System.out.print("-");
        }
        System.out.println();
    }
}

2.3 多个参数的定义

在方法后面的小括号(参数列表)中,也可以定义多个参数。多个参数用逗号隔开,在调用时要遵守定义的顺序。

public class Demo2 {
    public static void main(String[] args) {
        System.out.println("床前明月光,");
        int num = 10;
        printLine(num, "-"); // num为实参
        System.out.println("疑是地上霜。");
        printLine(5, "*");
        System.out.println("举头望明月,");
        printLine(8, "_");
        System.out.println("低头思故乡。");
        printLine(6, "~");
    }

    // 方法定义,在类的内部,与其他方法并列
    public static void printLine(int count, String sign) { // count为形参
        for (int i = 0; i < count; i++) {
            System.out.print(sign);
        }
        System.out.println();
    }
}

经验:根据业务的需求,来定义相应的方法参数。

// 案例:登录
public class Demo3 {
    public static void main(String[] args) {
        login("zhangsan", "123aaa");
    }

    /**
     * 登录方法
     * @param username 用户名
     * @param password 密码
     */
    public static void login(String username, String password) { 
        if(username == null || password == null) {
            System.out.println("用户名或密码为空");
        }else if(username.equals("zhangsan") && password.equals("123aaa")) {
            System.out.println("登录成功");
        }else {
            System.out.println("用户名或密码错误");
        }
    }
}

2.4 思考

如下代码,结果是:

基本数据在参数传递时都是值传递,即将值传到其他参数中

public class Demo4 {
    public static void main(String[] args) {
        int n = 5;
        m1(n);
        System.out.println(n); // 结果是?
    }

    public static void m1(int n) { 
        n++;
    }
}
public class Demo4 {
    public static void main(String[] args) {
//        int n = 5;
//        m1(n);
//        System.out.println(n); // 结果是5

        int a = 3;
        int b = 5;
        swap(a, b);
        System.out.println("交换后a=" + a + ", b=" + b);
    }

    // 基本数据在参数传递时都是值传递,即将值传到其他参数中
    public static void m1(int n) { 
        n++;
        System.out.println("方法中的n的值为:" + n);
    }

    public static void swap(int a, int b) {
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println("在方法中交换后a=" + a + ", b=" + b);
    }
}

三、方法的返回值

在方法执行过程中,有些方法可以没有返回值(存款),有些方法需要有返回值(取款)。

语法:

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

return 返回值;

}

public class Demo5 {
    public static void main(String[] args) {
        int n = sum(3, 5);
        System.out.println(n);
    }

    // 方法定义时int表示返回值类型
    public static int sum(int num1, int num2) {
        int sum = num1 + num2;
        return sum;
    }
}

注意:

  • 方法在定义返回值后,调用时可以使用变量接收,也可以不接收。
  • 方法定义了返回值后,在方法中必须返回,不能有条件返回。
  • return行后面不能再写其他代码。
  • 方法中不能出现可以同时执行的多个return。
  • 方法没有返回值也可以写return;
public class Demo6 {
    // 输出100以内所有的质数
    public static void main(String[] args) {
        for (int n = 2; n < 100; n++) {
            if(is(n)) {
                System.out.println(n + "是质数");
            }
        }
    }

    public static boolean is(int n) {
        for (int i = 2; i < n; i++) {
            if(n % i == 0) {
                return false;
            }
        }
        return true;
    }
}

四、方法的多级调用

在方法中可以调用其他方法,执行顺序还是根据代码的顺序执行,如果在代码执行过程中,有调用其他方法,会等该调用的方法执行完毕后,再执行后面的代码。

public class Demo7 {
    // 方法的多级调用
    public static void main(String[] args) {
        m1();
        // 最终显示的结果为:
        /*
        m1-start
        m2-start
        m2-end
        m1-end
        */
    }

    public static void m1() {
        System.out.println("m1-start");
        m2();
        System.out.println("m1-end");
    }

    public static void m2() {
        System.out.println("m2-start");
        System.out.println("m2-end");
    }
}

五、方法的递归

方法自己调用自己叫递归。如果在调用时无法停止,叫无穷递归,会报错。

public class Demo7 {
    // 无穷递归(报错)
    public static void main(String[] args) {
        m1();
    }

    public static void m1() {
        System.out.println("m1-start");
        m1();
        System.out.println("m1-end");
    }
}
public class Demo8 {
    // 求斐波拉契数列第n项的值
    public static void main(String[] args) {
        for (int i = 1; i <= 50; i++) {
            System.out.println("第"+i+"项为:" + f(i));
        }
    }
    // 1,1,2,3,5,8,13
    /*
     f(n) = 1; (n=1, n=2)
     f(n) = f(n-1) + f(n-2); (n > 2);
     */
    public static int f(int n) {
        if(n == 1 || n == 2) {
            return 1;
        }else {
            return f(n-1) + f(n-2);
        }
    }
}