1、什么是方法
2、方法的定义使用
1) 代码示例
package com.shen.operator;
public class Demo06 {
public static void main(String[] args) {
System.out.println(test(2,3));
}
public static int test (int num1,int num2){
int num = num1 > num2 ? num1 : num2;
return num;
}
}
2) 定义
什么是值传递(Java)和引用传递
参考 https://www.zhihu.com/question/31203609
3、方法的重载
1) 定义
2) 代码示例
```java package com.shen.operator;
public class Demo06 { public static void main(String[] args) {
double max_value = test(2,3);// 重载 方法名可相同,确保参数类型不同
System.out.println(max_value);
}
public static int test (int num1,int num2){
int num = num1 > num2 ? num1 : num2;
return num;
} public static double test (double num1,double num2,double num3){
double num = num1 > num2 ? num1 : num2;
System.out.println(num3);
return num;
} }
<a name="mX0sY"></a>
### 4、命令行传递参数
- 有时候希望在程序运行的时候再给他传递消息,就要靠命令行参数main()函数实现
<a name="pPUwg"></a>
#### 1) 代码示例
```java
package com;
public class Demo07 {
public static void main(String[] args) {
for (int i = 0; i < args.length;i ++){
System.out.println(i + ": " + args[i]);
}
}
}
2) 调用
javac Demo07.java
java com.shen.operator.Demo07 this is my wife
5、可变参数
package com.shen.operator;
public class Demo08 {
public static void main(String[] args) {
printMax(34,3,3,4,5,90,54);
printMax(new double[]{12,2,3,4,5,99,23});
}
public static void printMax(double... numbers){
if(numbers.length == 0){
System.out.println("No args passed");
return;
}
double result = numbers[0];
for(int i = 0;i < numbers.length; i++){
if(numbers[i] > result){
result = numbers[i];
}
}
System.out.println("The max value is " + result);
}
}
########################################################
The max value is 90.0
The max value is 99.0
进程已结束,退出代码为 0
6、递归
1) 定义
2)递归过程图示
3) 代码示例
package com.shen.operator;
public class Demo09 {
public static void main(String[] args) {
int fn_value = test(3);
System.out.println(fn_value);
}
public static int test(int num){
if (num == 1){
return 1;
}else{
return num * test(num -1 );
}
}
}