Java(7)方法,可变参数,方法重载

1 方法五要素

  • 方法 就是一段可以重复被执行的代码的封装
  • 方法定义的五要素:修饰符,返回值类型,方法名,参数列表,方法体;
  • public 访问修饰符
  • static 静态修饰符
  • void 返回值占位符 当方法没有返回值的时候使用void占位
  • main 方法名
  • String[] args 中 String[]参数的数据类型 args 参数名称相当与变量
  • { } 方法体
  • 可变参数 注意可变参数必须放在参数列表的最后一个位置

2 可变参数

public class Demo01 {
    public static void main(String[] args){
        System.out.println("123");
//123
        fun01();
        String fun02 = fun02();
        System.out.println(fun02);
        fun03("栗子");
        String fun04 =fun04("姓名");
        System.out.println(fun04);
        fun05(5,"包子");
        int fun06 = fun06(6,7);
        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("456");
    }//456

    /**
     * 没有参数有返回值
     * 返回值的数据类型与 return 的数据类型有关
     *  返回字符串
     */
    public static String fun02(){
        return ("789");
    }//789

    /**
     * 有参数没有返回值
     * 在定义方法的时候 该方法的参数 没有实际值 只是一个形式
     * 所以我们把这种表现形式的参数称之为形式参数 简称形参
     * food 食物
     */
    public static void fun03(String food){
        System.out.println("吃" + food);
    }//吃栗子

    /**
     * 有参数有返回值
     * name 姓名
     * 我的名字 是 name
     */
    public static String fun04(String name){
        return "我的名字是 >>> " + name;
    }//我的名字是 >>> 姓名

    /**
     * 两个参数没有返回值
     * count 数量
     * food  食物
     */
    public static void fun05(int count,String food){
        System.out.println("吃" + count + "个" + food);
    }//吃5个包子

    /**
     * 两个参数有返回值
     * a 加数
     * b 被加数
     * a + b 的和
     */
    public static int fun06(int a,int b){
        return a + b;
    }//13

    /**
     * 遍历输出数组
     * 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("]");
    }
    /**
[0, 1, 2, 3, 4]
[]
[]
    */

    /**
     * 遍历输出数组
     * 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("]");
    }
    /**
[0, 1, 2, 3, 4]
[]
[]
[]
[0, 1, 2, 3, 4]
    */

    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("]");
    }//a >>> 0	[1, 2, 3, 4]

}

3 方法重载

  • 方法重载
  • 在同一个了类中 方法参数列表不同的同名方法这种表现形式我们称之为方法重载
  • 参数列表 参数的数量 参数的数据类型
  • 此处参数的数据类型 指的相同参数位置上的数据类型
  • 此处参数的数据类型 指的相同参数位置上的数据类型
public class Demo02 {
    public static void main(String[] args) {
        eat();
        eat("栗子");
        eat("核桃",6);
        eat(5,"包子");
    }
    public static void eat(){
        System.out.println("吃吃吃");
    }//吃吃吃

    public static void eat(String food) {
        System.out.println("吃" + food);
    }//吃栗子

    public static void eat(String food,int count) {
        System.out.println("foodcount吃" + count +"个"+food);
    }//foodcount吃6个核桃

    public static void eat(int count,String food) {
        System.out. println("countfood!吃" + count +"个"+food);}
}//countfood!吃5个包子

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值