目录
修饰符 返回值类型 方法名称 ([ 参数类型 形参 ...]){方法体代码 ;[ return 返回值 ];}
- public class Method
- {
- // 方法定义
- public static boolean isLeapYear(int year)
- {
- if((0 == year % 4 && 0 != year % 100) || 0 == year % 400)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
【注意事项】定义方法的时候, 不会执行方法的代码. 只有调用的时候才会执行。一个方法可以被多次调用.
- public class Method
- {
- public static void main(String[] args)
- {
- int a = 10; int b = 20;
- System.out.println("第一次调用方法之前");
- int ret = add(a, b);
- System.out.println("第一次调用方法之后");
- System.out.println("ret = " + ret);
- System.out.println("第二次调用方法之前");
- ret = add(30, 50);
- System.out.println("第二次调用方法之后");
- System.out.println("ret = " + ret);
- }
- public static int add(int x, int y)
- {
- System.out.println("调用方法中 x = " + x + " y = " + y);
- return x + y;
- }
- }
// 执行结果一次调用方法之前调用方法中 x = 10 y = 20第一次调用方法之后ret = 30第二次调用方法之前调用方法中 x = 30 y = 50第二次调用方法之后ret = 80
- public class TestMethod
- {
- public static void main(String[] args)
- {
- int sum = 0;
- for (int i = 1; i <= 5; i++)
- {
- sum += fac(i);
- }
- System.out.println("sum = " + sum);
- }
- public static int fac(int n)
- {
- System.out.println("计算 n 的阶乘中n! = " + n);
- int result = 1;
- for (int i = 1; i <= n; i++)
- {
- result *= i;
- }
- return result;
- }
- }
// 执行结果计算 n 的阶乘中 n ! = 1计算 n 的阶乘中 n ! = 2计算 n 的阶乘中 n ! = 3计算 n 的阶乘中 n ! = 4计算 n 的阶乘中 n ! = 5sum = 153

- public static int getSum(int N)
- {
- // N是形参
- return (1+N)*N / 2;
- }
- getSum(10); // 10是实参,在方法调用时,形参N用来保存10
- public static int add(int a, int b)
- {
- return a + b;
- }
- add(2, 3); // 2和3是实参,在调用时传给形参a和b
注意:在Java中,实参的值永远都是拷贝到形参中,形参和实参本质是两个实体
- public class TestMethod
- {
- public static void main(String[] args)
- {
- int a = 10; int b = 20; swap(a, b);
- System.out.println("main: a = " + a + " b = " + b);
- }
- public static void swap(int x, int y)
- {
- int tmp = x;
- x = y;
- y = tmp;
- System.out.println("swap: x = " + x + " y = " + y);
- }
- }
// 运行结果swap : x = 20 y = 10main : a = 10 b = 20

注意:对于基础类型来说, 形参相当于实参的拷贝. 即 传值调用
- public class TestMethod
- {
- public static void main(String[] args)
- {
- int[] arr = {10, 20};
- swap(arr);
- System.out.println("arr[0] = " + arr[0] + " arr[1] = " + arr[1]);
- }
- public static void swap(int[] arr)
- {
- int tmp = arr[0];
- arr[0] = arr[1];
- arr[1] = tmp;
- }
- }
// 运行结果arr [ 0 ] = 20arr [ 1 ] = 10
- class Test
- {
- public static void main(String[] args)
- {
- int a = 10;
- int b = 20;
- print(a, b);
- }
- public static void print(int x, int y)
- {
- System.out.println("x = " + x + " y = " + y);
- }
- }
- public class TestMethod
- {
- public static void main(String[] args)
- {
- add(1, 2); // 调用add(int, int)
- add(1.5, 2.5); // 调用add(double, double)
- add(1.5, 2.5, 3.5); // 调用add(double, double, double)
- }
- public static int add(int x, int y)
- {
- return x + y;
- }
- public static double add(double x, double y)
- {
- return x + y;
- }
- public static double add(double x, double y, double z)
- {
- return x + y + z;
- }
- }
- // 注意:两个方法如果仅仅只是因为返回值类型不同,是不能构成重载的
- public class TestMethod
- {
- public static void main(String[] args)
- {
- int a = 10;
- int b = 20;
- int ret = add(a, b);
- System.out.println("ret = " + ret);
- }
- public static int add(int x, int y)
- {
- return x + y;
- }
- public static double add(int x, int y)
- {
- return x + y;
- }
- }
// 编译出错Test . java : 13 : 错误 : 已在类 Test 中定义了方法 add ( int , int )public static double add ( int x , int y ) {^1 个错误
- public class TestMethod
- {
- public static int add(int x, int y)
- {
- return x + y;
- }
- public static double add(double x, double y)
- {
- return x + y;
- }
- public static void main(String[] args)
- {
- add(1,2);
- add(1.5, 2.5);
- }
- }

方法签名中的一些特殊符号说明:



- public static void main(String[] args)
- {
- int n = 5;
- int ret = factor(n);
- System.out.println("ret = " + ret);
- }
- public static int factor(int n)
- {
- if (n == 1)
- {
- return 1;
- }
- return n * factor(n - 1); // factor 调用函数自身
- }
-
- // 执行结果 ret = 120

- public static void print(int n) {
- if(n < 10) {
- System.out.println(n);
- }else {
- print(n/10);
- System.out.println(n%10);
- }
- }
- public static int sumEvery(int n) {
- if(n < 10) {
- return n;
- }
- int tmp = n%10+sumEvery(n/10);
- return tmp;
- }
- public class Test {
- public static int count = 0;
-
- public static int fib(int n) {
- if(n == 1 || n == 2) {
- return 1;
- }
- if(n == 3) {
- count++;
- }
- int tmp = fib(n-1)+fib(n-2);
- return tmp;
- }
-
- public static int fib2(int n) {
- if(n <= 0) {
- return -1;
- }
- if(n == 1 || n == 2) {
- return 1;
- }
- int f1 = 1;
- int f2 = 1;
- int f3 = -1;
- for (int i = 3; i <= n; i++) {
- f3 = f1+f2;
- f1 = f2;
- f2 = f3;
- }
- return f3;
- }
-
- public static void main(String[] args) {
-
- int ret = fib(41);
- System.out.println(ret+" 次数:"+Test.count);
- }
- }