函数的学习

简介: 函数的学习

函数入门

函数重载

public class Demo01 {
    /*
    函数重载原则:
        方法名相同
        参数列表不相同
    注意:
        与返回值无关
     */
    public static void main(String[] args) {
        f01(1.0);
        f01(1);
    }
    public static void f01(double money) { // double
        System.out.println("f01(double money) 被调用了");
    }
    public static int f01(int age) { // int
        System.out.println("f01(int age) 被调用了");
        return 100;
    }
}

函数可变个数参数

import java.util.Arrays;
public class Demo02 {
    /*
    函数可变参数
     */
    public static void main(String[] args) {
        f01(new int[]{1,3,5,7,9});
        f01();
        f01(1);
        f01(1, 2);
        f01(1, 2, 3);
    }
    public static void f01(int... x) { // 总而言之,不管如何传参数,x最终是int[]一维数组
        System.out.println("f01(int...) 被调用了");
        System.out.println(Arrays.toString(x));
    }
}

foreach输出

public class Demo03 {
    /*
    函数可变参数
     */
    public static void main(String[] args) {
        f01(new int[]{1,3,5,7,9});
        f01();
        f01(1);
        f01(1, 2);
        f01(1, 2, 3);
    }
    public static void f01(int... arr) { // 总而言之,不管如何传参数,x最终是int[]一维数组
        System.out.println("f01(int...) 被调用了");
        // 自动循环arr数组,也知道何时结束,每次取出一个送到变量a上
        for (int a : arr) { // foreach输出(增强版for循环),没有下标了
            System.out.println(a);
        }
    }
}

传参 基本数据类型

public class Demo04 {
    public static void main(String[] args) {
        int y = 10;
        f01(y); // y实参。都是值传递。
        System.out.println("Y:" + y);
    }
    /*
        x,形参
     */
    public static void f01(int x) {
        System.out.println(x);
        x++;
        System.out.println(x);
    }
}

传参_引用数据类型

import java.util.Arrays;
public class Demo05 {
    public static void main(String[] args) {
        int[] y = {10, 20, 30};
        f01(y); // y实参。都是值传递。
        System.out.println("y: " + Arrays.toString(y));
    }
    /*
        x,形参
     */
    public static void f01(int[] x) {
        System.out.println(Arrays.toString(x));
        x[1]++;
        System.out.println(Arrays.toString(x));
    }
}

文件夹展示所有里面的文件

使用递归算法展示文件夹下所有文件

import java.io.File;
public class Demo06 {
    public static void main(String[] args) {
        showFiles(new File("E:\\202203\\计科\\函数\\学习\\1"));
    }
    /*
        展示文件夹下所有的文件,包含子文件夹下的文件
     */
    public static void showFiles(File file) {
        File[] files = file.listFiles(); // 枚举该文件夹file对象下的文件或文件夹对象
        for (File f : files) {
            if (f.isFile()) System.out.println(f.getName()); // 文件
            if (f.isDirectory()) showFiles(f); // 递归调用文件夹
        }
    }
}

1加到100的递归调用

public class Demo07 {
    public static void main(String[] args) {
        System.out.println(sum(100));
    }
    public static int sum(int count) {
        if (count == 1) return 1;// 结束条件
        return sum(count-1)+count; // 递归表达式
    }
}

下载链接

https://llzai.lanzoum.com/i3luM14jt6je

相关文章
|
5月前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
40 1
|
11月前
|
Python
Python函数的参数学习
Python函数的参数学习
52 0
|
2月前
|
存储 容器
函数的学习与使用
函数的学习与使用
|
3月前
|
存储 编译器 文件存储
|
5月前
|
Java Python
编程中的函数与方法
编程中的函数与方法
53 4
基础知识 函数
基础知识 函数
65 0
|
5月前
|
C++
第三章:C++中的函数
第三章:C++中的函数
36 1
|
程序员
学C的第九天(深入学习函数:库函数、自定义函数、函数的参数、函数调用、练习、补充知识点)-2
5.5:练习(部分上一期做过,换成函数做法): (1).写一个函数可以判断一个数是不是素数:
|
程序员 编译器 C语言
学C的第九天(深入学习函数:库函数、自定义函数、函数的参数、函数调用、练习、补充知识点)-1
1.函数是什么: 维基百科中对函数的定义:子程序 * 在计算机科学中,子程序(英语: