什么是方法、方法的定义、方法调用、方法重载、命令行传参、可变参数

一、何谓方法?

  • System.out.println(),那么它是什么呢?
    System:类
    out:对象
    println():方法
  • Java方法是语句的集合,它们在一起执行一个功能。
    1. 方法是解决一类问题的步骤的有序组合
    2. 方法包含与于类或对象中
    3. 方法在程序中布尔创建,在其他地方被引用
  • 设计方法的原则:方法的本意是功能块,就是实现某个功能的语句块的集合。我们设计方法的时候,最好保持方法的原子性,就是一个方法只完成一个功能,这样有利于我们后期的扩展

代码1

package com.jacyzhu.method;

public class Demo01 {
    // main方法
    public static void main(String[] args) {
        int sum = add(1, 2);
        System.out.println(sum);
    }

    // 加法
    public static int add(int a, int b){
        return a + b;
    }
}
运行结果:
3

代码2

package com.jacyzhu.method;

public class Demo01 {
    // main方法
    public static void main(String[] args) {
//        int sum = add(1, 2);
//        System.out.println(sum);
        test();
    }

    // 加法
    public static int add(int a, int b){
        return a + b;
    }

    public static void test(){
        for (int i = 0; i <= 1000; i++) {
            if (i % 5 == 0){
                System.out.print(i + "\t");
            }
            if (i % (5*3) == 0){
                System.out.println();
                // System.out.println("\n");
            }
        }
    }
}
运行结果:
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	

二、方法的定义

  • Java的方法类似于其他语言的函数,是一段用来完成特定功能的代码片段,一般情况下,定义一个方法包含以下语法:
  • 方法包含一个方法头和一个方法体。下面是一个方法的所有部分:
    1. 修饰符:修饰符,这是可选的,告诉编译器如何盗用该方法。定义了该方法的访问类型。
    2. 返回值类型:方法可能会返回值,returnValueType是方法返回值的数据类型。有些方法执行所需的操作,但没有返回值。在这种情况下,returnValueType是关键字void。
    3. 方法名:是方法的实际名称。方法名和参数表共同构成方法签名。
    4. 参数类型:参数像是一个占位符。放方法被调用时,传递值给参数。这个值被称为实参或变量参数列表是指方法的参数类型。顺序和参数的个数。参数是可选的,方法可以不包含任何参数。
      • 形式参数:在方法被调用时用于接收外界输入的数据。
      • 实参:调用方法时实际传给方法的数据。
    5. 方法体:方法包含具体的 语句,定义该方法的 功能。
修饰符 返回值类型 方法名(参数类型 参数名){
	...
	方法体
	...
	return 返回值;
}

形参和实参

package com.jacyzhu.method;

public class Demo01 {
    // main方法
    public static void main(String[] args) {
        // 实际参数:实际调用传递给它的参数
        int sum = add(1, 2);
        System.out.println(sum);
        // test();
    }

    // 加法
    // 形式参数:用来定义作用的
    public static int add(int a, int b){
        return a + b;
    }

    public static void test(){
        for (int i = 0; i <= 1000; i++) {
            if (i % 5 == 0){
                System.out.print(i + "\t");
            }
            if (i % (5*3) == 0){
                System.out.println();
                // System.out.println("\n");
            }
        }
    }
}

比较大小

package com.jacyzhu.method;

public class Demo02 {
    public static void main(String[] args) {

        int max = max(10, 20);
        System.out.println(max);
    }

    // 比大小
    public static int max(int num1, int num2){

        int result = 0;

        if (num1 == num2){
            System.out.println("num1==num2");
            return 0; // 终止方法
        }

        if (num1 > num2) {
            result =  num1;
        }else {
            result = num2;
        }

        return result;
    }
}
运行结果120
运行结果2:
num1==num2
0

三、方法调用

  • 调用方法:对象名.方法名(实参 列表)
  • Java支持两种调用方法的方式,根据方法是否返回值来选择
  • 当方法返回一个值的时候,方法调用通常被当做一个值。例如:
int larger = max(10, 20);
  • 如果方法返回值是void,方法调用一定是一条语句。
System.out.println("Hello, Jacy!");
  • 值传递(Java)和引用传递

      在程序设计语言中将参数传给方法或函数的方式目前有两种:一种是值传递,一种是引用传递。
      值传递表示将实参的值传递给方法;
      引用传递表示将实参a的地址引用传递给方法。
      Java是采用值传递的,Java程序中的方法得到的总是实参值的拷贝。
    

四、方法的重载

  • 重载就是在一个类中,有相同的函数名称,但形参不同的函数。
  • 方法的重载规则:
    1. 方法名称必须相同
    2. 参数列表必须不同(个数不同、或类型不同、参数排列顺序不同等)
    3. 方法的返回类型可以相同也可以不相同
    4. 仅仅返回类型不同不足以称为方法的重载
  • 实现理论:
    方法名称相同时,编译器会根据调用方法的参数个数、参数类型等去逐个匹配,以选择对应的方法,如果匹配失败,则编译器报错。

比较大小方法重载

package com.jacyzhu.method;

public class Demo02 {
    public static void main(String[] args) {

        double max = max(20.0, 30.0);
        System.out.println(max);
    }

    // 比大小
    public static double max(double num1, double num2){

        double result = 0;

        if (num1 == num2){
            System.out.println("num1==num2");
            return 0; // 终止方法
        }

        if (num1 > num2) {
            result =  num1;
        }else {
            result = num2;
        }

        return result;
    }

    // 比大小
    public static int max(int num1, int num2){

        int result = 0;

        if (num1 == num2){
            System.out.println("num1==num2");
            return 0; // 终止方法
        }

        if (num1 > num2) {
            result =  num1;
        }else {
            result = num2;
        }

        return result;
    }
}
运行结果:
30.0

加法方法重载

package com.jacyzhu.method;

public class Demo01 {
    // main方法
    public static void main(String[] args) {
        // 实际参数:实际调用传递给它的参数
        int sum = add(1, 2, 3);
        System.out.println(sum);
        // test();
    }

    // 加法
    // 形式参数:用来定义作用的
    public static int add(int a, int b, int c){
        return a + b + c;
    }

    public static int add(int a, int b){
        return a + b;
    }

    public static void test(){
        for (int i = 0; i <= 1000; i++) {
            if (i % 5 == 0){
                System.out.print(i + "\t");
            }
            if (i % (5*3) == 0){
                System.out.println();
                // System.out.println("\n");
            }
        }
    }
}
运行结果:
6

五、命令行传参

  • 有时候你希望运行一个程序时再传递给它消息。这要靠传递命令行参数给main()函数实现。
package com.jacyzhu.method;

public class Demo03 {
    public static void main(String[] args) {
        // args.length  数组长度
        for (int i = 0; i < args.length; i++) {
            System.out.println("args[" + i + "]" + args[i]);
        }
    }
}

在这里插入图片描述

六、可变参数

  • JDK1.5开始,Java支持传递同类型的可变参数给一个方法
  • 在方法声明中,在指定参数类型后加一个省略号(…)
  • 一个方法中只能指定一个可变参数,它必须是方法的最后一个参数。任何普通的参数必须在它之前声明
package com.jacyzhu.method;

public class Demo04 {
    public static void main(String[] args) {
        Demo04 demo04 = new Demo04();
        demo04.test(1, 2, 3, 4, 5, 6);
    }
    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]);
        System.out.println(i[5]);
    }
}
运行结果:
1
2
3
4
5
6

最大值

package com.jacyzhu.method;

public class MaxDemo04 {
    public static void main(String[] args) {
        // 调用可变参数的方法
        printMax(34, 3, 3, 2, 56.5);
        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 56.5
The max value is 3.0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值