[修饰符] 返回类型 函数名( [参数列表] ){ 方法体 return [值]; }
package base.class1; public class Function { public static void main(String[] args) { int res = add(2, 3); System.out.println("res=" + res); } public static int add(int a, int b) { return a + b; } }
public static int add(int a, int b) { return a + b; } public static float add(float a, float b) { return a + b; } public static int add(int a, int b, int c) { return a + b + c; }
package base.class1; public class Function { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println("args[" + i + "]" + args[i]); } } }
PS E:\JavaPro\Demo\Demo> javac .\src\base\class1\Function.java PS E:\JavaPro\Demo\Demo> cd src PS E:\JavaPro\Demo\Demo\src> java base.class1.Function hi 123 args[0]hi args[1]123
package base.class1; public class Function { public static void main(String[] args) { max(); max(1, 2, 3); } public static void max(int... num) { int res = 0; if (num.length == 0) { System.out.println("para null"); return; } for (int i = 0; i < num.length; i++) { if (num[i] > res) { res = num[i]; } } System.out.println("max=" + res); } }
para null max=3
1 | 1 |
2 | 2*1 |
3 | 3*2*1 |
... | ... |
n | n*(n-1)*...3*2*1 |
public static int fn(int n) { if (n == 0) { return 1; } else { return n * fn(n - 1); } }