java方法

一、方法的概念

方法:就是一段可以重复被执行的代码的封装

二、方法的五要素

方法定义的五要素:修饰符,返回值类型,方法名,参数列表,方法体;

public   访问修饰符
static   静态修饰符
void     返回值占位符 当方法没有返回值的时候使用void占位
main     方法名
String[] args 中  String[]参数的数据类型 args 参数名称相当与变量
 { }       方法体

public class Dome01 {
    public static void main(String[] args) {
        System.out.println("出淤泥而不染");
        fun01();
        String fun02 = fun02();
        System.out.println(fun02);
        fun03("毛嗑");
        String fun04 = fun04("DDL");
        System.out.println(fun04);
        fun05(2,"馅饼");
        int fun06 = fun06(100,200);
        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(0, 1, 2, 3, 4);
    }

    /**
     * 没有参数没有返回值
     */
    public static void fun01(){
        System.out.println("床前明月光,疑是地上霜");
    }

    /**
     * 没有参数有返回值
     * @return
     */
    public static String fun02() {
        return "濯清涟而不妖";
    }

    /**
     * 有参数没有返回值
     * @param food 食物
     */
    public static void fun03(String food) {
        System.out.println("吃" + food);
    }
    /**
     * name 是姓名
     * 有参数有返回值
     */
    public static String fun04(String name){
        return "我的名字是 >>> " + name;
    }
    /**
     * 两个参数没有返回值
     * count 数量
     * food 食物
     */
    public static void fun05(int count,String food){
        System.out.println("吃" + count + "个" + food);
    }
    /**
     * 两个参数有返回值
     */
    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("]");
    }

}

返回值的数据类型与 return 的数据类型有关。

在定义方法的时候该方法的参数没有实际值只是一个形式所以我们把这种表现形式的参数称之为形式参数,简称形参。 

在调用有参方法的时候传递给方法的参数为实际有真实值的参数,那么这个参数我们称之为实际参数,简称实参。

实参是数据类型必须与形参一致或形参的子类型。

注意:有一中特殊的实参为引用类型。

可变参数:注意可变参数必须放在参数列表的最后一个位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值