方法的定义
java的方法类似于其他语言的函数,是一段用来完成特定功能的代码片段,一般情况下,定义一个方法包含以下语法:
方法包含一个方法头和一个方法体。下面是一个方法的所有部分:
- 修饰符:修饰符,这是可选的,告诉编译器如何调用该方法。定义了该方法的访问类型。
- 返回值类型:方法可能会返回值。returnValueType是方法返回值的数据类型。有些方法执行所需的操作,但没有返回值。这种情况下,returnValueType是关键字void。
- 方法名:是方法的实际名称。方法名和参数共同构成方法的签名。
- 参数类型:参数像是一个占位符。当方法被调用时,传递值给参数。这个值被称为实参或变量。参数列表是指方法的参数类型、顺序和参数的个数。参数是可选的,方法可以不包含任何参数。
- 形式参数:在方法被调用时用于接收外界输入的数据。
- 实参:调用方法时实际传给方法的数据。
- 方法体:方法体包含具体的语句,定义该方法的功能。
修饰符 返回值类型 方法名(参数类型 参数名){
···
方法体
···
return 返回值;
}
方法的重载
重载就是在一个类中,有相同的函数名称,但形参不同的函数。
方法的重载规则:
- 方法名称必须相同
- 参数列表必须不同(个数不同、或类型不同、参数排列顺序不同等)
- 方法的返回类型可以相同也可以不相同
- 仅仅返回类型不同不足以成为方法的重载
实现理论:
方法名称相同时,编译器会根据调用方法的参数个数、参数类型等去逐个匹配,以选择对应的方法,如果匹配失败,则编译器报错。
命令行传参
可变参数
递归
代码
package com.method;
/**
* ━━━━━━神兽出没━━━━━━
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃
* ┗━┓ ┏━┛Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* <p>
* ━━━━━━感受萌萌哒━━━━━━
*
* @author 5rW3
* version 1.0
* since 1.8
*/
public class Demo01 {
//main方法
public static void main(String[] args) {
// 实际参数:实际调用传递给他的参数
// int sum = add(1,2);
// System.out.println(sum);
test();
}
//加法
public static int add(int a,int b){
return a+b;
}
public static void test(){
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;
}
System.out.print(i);
}
}
}
package com.method;
/**
* ━━━━━━神兽出没━━━━━━
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃
* ┗━┓ ┏━┛Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* <p>
* ━━━━━━感受萌萌哒━━━━━━
*
* @author 5rW3
* version 1.0
* since 1.8
*/
public class Demo02 {
public static void main(String[] args) {
double max = max(10.0,20.0);
System.out.println(max);
}
//比大小
public static int max(int num1,int num2){
int result = 0;
if (num1==num2){
System.out.println("num1==num2");
return 0;//终止方法
}
if (num1>num2){
result = num1;
}else{
result = num2;
}
return result;
}
public static double max(double num1,double num2){
double result = 0;
if (num1==num2){
System.out.println("num1==num2");
return 0;//终止方法
}
if (num1>num2){
result = num1;
}else{
result = num2;
}
return result;
}
}
package com.method;
/**
* ━━━━━━神兽出没━━━━━━
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃
* ┗━┓ ┏━┛Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* <p>
* ━━━━━━感受萌萌哒━━━━━━
*
* @author 5rW3
* version 1.0
* since 1.8
*/
public class Demo03 {
public static void main(String[] args) {
Demo03 test = new Demo03();
test.test();
}
public void test(){
test();
}
}
package com.method;
/**
* ━━━━━━神兽出没━━━━━━
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃
* ┗━┓ ┏━┛Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* <p>
* ━━━━━━感受萌萌哒━━━━━━
*
* @author 5rW3
* version 1.0
* since 1.8
*/
public class Demo04 {
public static void main(String[] args) {
System.out.println(f(5));
}
public static int f(int n){
if (n==1){
return 1;
}else{
return n*f(n-1);
}
}
}