java方法详解

时间:2021-06-02 20:22:55   收藏:0   阅读:0

java方法详解

一、何为方法?

? 调用系统类里面的标准输出对象out中的方法叫做 println方法

结果:3


package com.lv.method;

public class Demo02 {
    public static void main(String[] args) {
        test();
    }
    public static void test(){
        for (int i = 0; i <=1000; i++) {
            if(i%5==0){
                System.out.print(i+"  ");
            }
            if (i%(5*3)==0){
                System.out.println();
            }

        }
    }
}

结果:

0
5 10 15
20 25 30
35 40 45
50 55 60
65 70 75
80 85 90
95 100 105
110 115 120
125 130 135
140 145 150
155 160 165
170 175 180
185 190 195
200 205 210
215 220 225
230 235 240
245 250 255
260 265 270
275 280 285
290 295 300
305 310 315
320 325 330
335 340 345
350 355 360
365 370 375
380 385 390
395 400 405
410 415 420
425 430 435
440 445 450
455 460 465
470 475 480
485 490 495
500 505 510
515 520 525
530 535 540
545 550 555
560 565 570
575 580 585
590 595 600
605 610 615
620 625 630
635 640 645
650 655 660
665 670 675
680 685 690
695 700 705
710 715 720
725 730 735
740 745 750
755 760 765
770 775 780
785 790 795
800 805 810
815 820 825
830 835 840
845 850 855
860 865 870
875 880 885
890 895 900
905 910 915
920 925 930
935 940 945
950 955 960
965 970 975
980 985 990
995 1000


二、方法的定义

? 方法包含一个方法头和一个方法体。下面是一个方法的所有部分:

? 1.修饰符:修饰符,这是可选的,告诉编译器如何调用该方法。定义了该方法的访问类型。

    2. 方法可能会返回值。returnValueType是方法返回值的数据类型。有些方法执行所需,但没有返回值。在这种情况下,returnValueType是关键字void。
       3. 方法名:是方法的实际名称。方法名和参数表共同构成方法签名。
       4. 参数类型:参数像是一个占位符。当方法被调用时,传递值给参数。这个值被称为实参或变量。参数列表是指方法的参数类型、顺序和参数的个数。参数是可选的,方法可以不包含任何参数。

? (1)形式参数:在方法被调用时用于接收外界输入的数据。

? (2)实参:调用方法时实际传给方法的数据。

  1. 方法体:方法体包含具体的语句,定义该方法的
修饰符 返回值类型 方法名(参数类型  参数名){
    ...
    方法体
    ...
    return 返回值;
}

方法调用

int large = max(30,40);
System.out.println("Hello,World!")
package com.lv.method;

public class Demo03 {
    public static void main(String[] args) {
        int max = max(10, 20);
        System.out.println(max);

    }
    //比大小
    public static int max(int num1,int num2 ){
        int resout = 0;
        if (num1==num2){
            System.out.println("num1==num2");
            return 0;
        }
        if (num1>num2){
            resout = num1;
            return resout;
        }
        else {
            resout = num2;
            return resout;

        }

    }
}

结果: 20

三、方法的重载

  1. 方法名称必须相同。
  2. 参数列表必须不同(个数不同、或类型不同、参数排列顺序不同等)。
  3. 方法的返回类型可以相同也可以不同。
  4. 仅仅返回类型不同不足以成为方法的重载。
package com.lv.method;

public class Demo03 {
    public static void main(String[] args) {
    double max = max(10.0, 10.1);
        System.out.println(max);

    }
    //比大小
    public static double max(double num1,double num2 ) {
        double resout = 0;
        if (num1 == num2) {
            System.out.println("num1==num2");
            return 0;
        }
        if (num1 > num2) {
            resout = num1;
            return resout;
        } else {
            resout = num2;
            return resout;

        }
    }
    //比大小
    public static int max(int num1,int num2 ){
        int resout = 0;
        if (num1==num2){
            System.out.println("num1==num2");
            return 0;
        }
        if (num1>num2){
            resout = num1;
            return resout;
        }
        else {
            resout = num2;
            return resout;

        }

    }
}

结果:10.1


方法名称相同时,编译器会根据调用方法的参数个数、参数类型等去逐个匹配,以选择对应得方法,如果匹配失败,则编译器报错。

四、命令行传参

public class CommandLine{
    public static void main(String arge[]){
        for(int i =0; i<args.lengt;i++){
       System.out.println("args["+i+"]:"+args[i]);
        }
    }
}

1.打开储存代码的文件夹

技术图片

2.在此打开cmd,并输入

技术图片

五、可变参数

package com.lv.method;

public class Demo05 {![image](https://img2020.cnblogs.com/blog/2363286/202106/2363286-20210601193755372-589756523.jpg)
    public static void main(String[] args) {
        Demo05 demo05 = new Demo05();
        demo05.test(1,2,3,4,5);
    }

    public void test(int...i){
        System.out.println(i[0]);
        System.out.println(i[1]);
        System.out.println(i[2]);
        System.out.println(i[3]);
        System.out.println(i[4]);
    }
}

结果:

1
2
3
4
5


package com.lv.method;

public class Demo06 {
    public static void main(String[] args) {
        printmax(34,54,60.4,8);
        printmax(new double[]{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<numbers.length;i++){
            if(numbers[i]>result){
                result=numbers[i];
            }
    }
        System.out.println("The max value is:"+result);
    }
}

结果:

The max value is:60.4
The max value is:3.0

六、递归

  1. 递归头:什么时候不调用自身方法。如果没有头,将陷入死循环。
  2. 递归体:什么时候需要调用自身方法。
package com.lv.method;

public class Demo07 {
    public static void main(String[] args) {
        System.out.println(f(5));
    }
    //1!   1
    //2!   2*1
    //5!    5*4*3*2*1


    //2   2*f(1)
    //3   3*f(2)

    public static int f(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n * f(n - 1) ;
            }
        }

    }

结果:120


原理:

技术图片

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!