方法概述
- 方法(Method): 是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集
- 方法必须先创建才可以使用,该过程称为方法定义
- 方法创建后并不是直接运行的,需要手动使用后才执行,该过程称为方法调用
方法的定义和调用
- 格式 :
// 1.方法的定义
public static void 方法名(){
// 方法体
}
// 2.方法的调用
方法名();
- 代码测试 :
public class Test {
// 1.定义一个方法,判断方法内的数据是否为偶数
public static void isEvenNumber() {
int number = 10;
System.out.println(number % 2 == 0 ? "偶数" : "奇数");
}
public static void main(String[] args) {
// 2.调用方法
isEvenNumber();
}
// 3.方法必须先定义后调用
}
- 方法练习 :
import java.util.Scanner;
public class Test {
/**
* 设计一个方法用于打印两个数中的较大数
**/
// 1.定义一个方法,通过控制台输入两个数,判断较大数,并进行输出
public static void getMax() {
Scanner input = new Scanner(System.in);
System.out.println("输入第一个数值:");
int a = input.nextInt();
System.out.println("输入第二个数值:");
int b = input.nextInt();
System.out.println("两数最大值为:" + (a > b ? a : b));
}
public static void main(String[] args) {
// 2.调用方法
getMax();
}
}
带参数方法的定义和调用
-
方法格式 :
- 方法定义时,参数中的 数据类型 与 变量名 都不能缺少,缺失任意一个程序都将报错
- 方法定义时,多个参数之间使用 逗号< , > 分隔
- 方法调用时,参数的 数量 与 类型 必须与方法定义时设置相匹配,否则程序将报错
// 1.定义格式
public static void 方法名(参数){
.......
}
// 单个参数
public static void 方法名(数据类型 变量名){
.......
}
// 多个参数
public static void 方法名(数据类型 变量名一, 数据类型 变量名二, .......){
.......
}
// 2.调用格式
方法名(参数);
// 单个参数调用
方法名(变量名/常量值);
// 多个参数调用
方法名(变量名一/常量值一,变量名二/常量值二,.......);
- 形参和实参 :
形参 :方法定义中的参数 <数据类型 变量名>
实参 :方法调用中的参数 <变量名/常量值> - 代码测试 :
public class Test {
// 1.定义一个判断大小的有参方法,输出最大值
public static void getMax(int a, int b) {
System.out.println("最大值为:" + (a > b ? a : b));
}
public static void main(String[] args) {
// 2.定义两个 int 类型的变量并赋值
int a = 10;
int b = 20;
// 3.调用方法,并为方法赋值
getMax(a, b);
getMax(30, 40);
}
}
带返回值方法的定义和调用
- 方法格式 :
方法的返回值通常会使用变量接受,否则该返回值将无意义
// 1.定义格式
public static 数据类型 方法名(参数){
return 数据;
}
// 2.调用格式
数据类型 变量名 = 方法名(参数);
- 代码测试 :
public class Test {
// 1.定义一个有参带返回值的方法,返回较大值
public static int getMax(int a, int b) {
return a > b ? a : b;
}
public static void main(String[] args) {
// 2.定义两个 int 类型的变量并赋值
int a = 10;
int b = 20;
// 3.调用方法
int c = getMax(a, b);
System.out.println("最大值为:" + c);
System.out.println("最大值为:" + getMax(a, b));
}
}
方法注意事项
- 方法不能嵌套定义,方法是平级关系
- void表示无返回值,可以省略 returm,也可以单独书写 returm ,后面不加数据
- returm 表示终止运行,返回数据 / 不返回数据
方法重载
-
方法重载概叙 :
方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载
- 多个方法在同一个类中
- 多个方法具有相同的方法名
- 多个方法的参数不相同,类型不同或者数量不同
-
方法重载特点 :
重载仅对应方法的定义,与方法的调用无关,调用方法参照标准格式
重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,不能通过返回值来判断多个方法是否相互构成重载 -
代码测试 :
public class Test {
// 1.加法计算的方法定义
public static int sum(int a, int b) {
return a + b;
}
public static double sum(double a, double b) {
return a + b;
}
public static int sum(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
// 2.加法计算的方法调用
int result1 = sum(10, 20);
System.out.println(result1);
double result2 = sum(10.0, 20.0);
System.out.println(result2);
int result3 = sum(10, 20, 30);
System.out.println(result3);
}
}
- 代码练习 :
public class Test {
public static boolean sum(int a, int b) {
System.out.println("int");
return a == b;
}
public static boolean sum(byte a, byte b) {
System.out.println("byte");
return a == b;
}
public static boolean sum(short a, short b) {
System.out.println("short");
return a == b;
}
public static boolean sum(long a, long b) {
System.out.println("long");
return a == b;
}
public static void main(String[] args) {
// 判断所有类型的整数是否相同
System.out.println(sum(10, 20));
System.out.println(sum((byte) 10, (byte) 20));
System.out.println(sum((short) 10, (short) 20));
System.out.println(sum(10L, 20L));
}
}
方法的参数传递
- 基本类型 :
public class Test {
// 对于基本数据类型的参数,形式参数的改变,不影响实际参数的值
public static void main(String[] args) {
int number = 100;
System.out.println("调用change方法前,number = " + number);
change(number);
System.out.println("调用change方法后,number = " + number);
}
public static void change(int number) {
number = 200;
}
}
- 引用类型 :
public class Test {
// 对于引用数据类型的参数,形式参数的改变,影响实际参数的值
public static void main(String[] args) {
int[] arr = { 10, 20, 30 };
System.out.println("调用change方法前,arr[1] = " + arr[1]);
change(arr);
System.out.println("调用change方法后,arr[1] = " + arr[1]);
}
public static void change(int[] arr) {
arr[1] = 200;
}
}
案例练习
- 案例一 :
public class Test {
// 数组遍历
public static void main(String[] args) {
int[] arr1 = { 10, 20, 30 };
int[] arr2 = { 100, 200, 300 };
printArray(arr1);
printArray(arr2);
}
public static void printArray(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr[i]);
} else {
System.out.print(arr[i] + ", ");
}
}
System.out.println("]");
}
}
- 案例二 :
public class Test {
// 数组最大值
public static void main(String[] args) {
int[] arr1 = { 10, 20, 30 };
int[] arr2 = { 30, 20, 10 };
int[] arr3 = { 20, 10, 30 };
System.out.println("数组arr1最大值为:" + maximumArray(arr1));
System.out.println("数组arr2最大值为:" + maximumArray(arr2));
System.out.println("数组arr3最大值为:" + maximumArray(arr3));
}
public static int maximumArray(int[] arr) {
int maximum = arr[0];
for (int i = 1; i < arr.length; i++) {
if (maximum < arr[i]) {
maximum = arr[i];
}
}
return maximum;
}
}