【调用方法】

  • 方法 就是一段可以重复被执行的代码的封装
  • 方法定义的五要素:修饰符,返回值类型,方法名,参数列表,方法体;
  • public 访问修饰符
  • static 静态修饰符
  • void 返回值占位符 当方法没有返回值的时候使用void占位
  • main 方法名
  • String[] args 中 String[]参数的数据类型 args 参数名称相当与变量
  • {} 方法体
  • 可变参数 注意可变参数必须放在参数列表的最后一个位置
javascript
public class Demo01 {
    public static void main(String[] args) {
        System.out.println("天行健,君子以自强不息。");
        fun01();
        String fun02 = fun02();
        System.out.println(fun02);
        // 在调用有参方法的时候传递给方法的参数为实际有真实值的参数 那么这个参数我们称之为实际参数 简称实参
        // 注意有一中特殊的实参 引用类型
        // 实参是数据类型必须与形参一致或形参的子类型
        fun03("水果");
        String fun04 = fun04("迪丽热巴");
        System.out.println(fun04);
        fun05(1, "馄饨");
        int fun06 = fun06(3, 3);
        System.out.println(fun06);
        fun07(new int[]{0, 1, 2, 3, 4});
        fun07(new int[]{});
        fun07(null);
        //括号中,应该根据要求提供参数,不提供,或者提供多个参数都会报错
        // fun07();
        // fun07(0, 1, 2, 3, 4);
        //可变参数  将提供的的参数,默认转变为数组
        fun08(new int[]{0, 1, 2, 3, 4});
        fun08(new int[]{});
        fun08(null);
        fun08();
        fun08(0, 1, 2, 3, 4);
        fun09(01, 2, 3, 4);
    }
    /**
     * 没有参数没有返回值
     */
    public static void fun01() {
        System.out.println("我喜欢你");
    }
    /**
     * 没有参数有返回值
     * 返回值的数据类型与 return 的数据类型有关
     *
     * @return 返回字符串
     */
    public static String fun02() {
        return "天行健,君子以自强不息。";
    }
    /**
     * 有参数没有返回值
     * 在定义方法的时候 该方法的参数 没有实际值 只是一个形式 所以我们把这种表现形式的参数称之为形式参数 简称形参
     *
     * @param food 食物
     */
    public static void fun03(String food) {
        System.out.println("吃" + food);
    }
    /**
     * 有参数有返回值
     *
     * @param name 姓名
     * @return 我喜欢的人是 name
     */
    public static String fun04(String name) {
        return "我喜欢的人是 >>> " + name;
    }
    /**
     * 两个参数没有返回值
     *
     * @param count 数量
     * @param food  食物
     */
    public static void fun05(int count, String food) {
        System.out.println("吃" + count + "碗" + food);
    }
    /**
     * 两个参数有返回值
     *
     * @param a 加数
     * @param b 被加数
     * @return a + b 的和
     */
    public static int fun06(int a, int b) {
        return a + b;
    }
    /**
     * 遍历输出数组
     *
     * @param array 被遍历的数组
     */
    public static void fun07(int[] array) {
        System.out.print("[");
        //当数组不为空时
        if (array != null) {
            for (int i = 0; i < array.length; i++) {
                if (i < array.length - 1) {
                    System.out.print(array[i] + ", ");
                } else {
                    System.out.print(array[i]);
                }
            }
        }
        System.out.println("]");
    }
    /**
     * 遍历输出数组
     *
     * @param array 被遍历的数组 可变参数 注意可变参数必须放在参数的最后一个位置
     */
    public static void fun08(int... array) {
        System.out.print("[");
        if (array != null) {
            for (int i = 0; i < array.length; i++) {
                if (i < array.length - 1) {
                    System.out.print(array[i] + ", ");
                } else {
                    System.out.print(array[i]);
                }
            }
        }
        System.out.println("]");
    }
    public static void fun09(int a ,int... array) {
        System.out.print("a >>> " + a + "\t");
        System.out.print("[");
        if (array != null) {
            for (int i = 0; i < array.length; i++) {
                if (i < array.length - 1) {
                    System.out.print(array[i] + ", ");
                } else {
                    System.out.print(array[i]);
                }
            }
        }
        System.out.println("]");
    }
}

天行健,君子以自强不息。
//fun01输出结果
我喜欢你
//fun02输出结果
天行健,君子以自强不息。
//fun03输出结果
吃水果
//fun04输出结果
我喜欢的人是 >>> 迪丽热巴
//fun05输出结果1碗馄饨
//fun06输出结果
6
//fun07输出结果
[0, 1, 2, 3, 4]
[]
[]
//fun08输出结果
[0, 1, 2, 3, 4]
[]
[]
[]
[0, 1, 2, 3, 4]
//fun09输出结果
//注意我们给出了5个参数,但时没有定义参数类型
//因此默认将第一个参数定义为数组a,而后面的为可变参数类型,所以将后面参数全部定义为同一个数组
a >>> 0		[1, 2, 3, 4]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值