1.方法定义
Java方法是语句的集合,他们在一起执行一个功能
2.方法使用
public class Test02 { public static int sum(int a,int sum){ for (int i = 0; i < 101; i++) { sum+=i; } return sum; } public static void main(String[] args) { int res= sum(1,0); System.out.println(res); } }
3.方法重载
public class Overload { public static void sum(int a){ a=10; System.out.println(a); } public static void sum(int a,int b){ a=10; System.out.println(a); } public static void sum(double b,int a){ a=10; System.out.println(a); } public static void sum(int a,double b){ a=10; System.out.println(a); } }
4.递归
public class Test01 { public static int f(int n){ if (n==1){ return 1; }else { return n*f(n-1); } } public static void main(String[] args) { System.out.println(f(5)); } }