1. 方法概述
1.1 方法的概念
方法(method)是程序中最小的执行单元
- 注意:
- 方法必须先创建才可以使用,该过程成为方法定义
- 方法创建后并不是直接可以运行的,需要手动使用后,才执行,该过程成为方法调用
2. 方法的定义和调用
2.1 无参数方法定义和调用
-
定义格式:
public static void 方法名 ( ) { // 方法体; }
-
调用格式:
方法名();
-
注意:
方法必须先定义,后调用,否则程序将报错
2.3 无参数方法的练习
- 需求:设计一个方法用于打印两个数中的较大数
- 思路:
- 定义一个方法,用于打印两个数字中的较大数,例如getMax()
- 方法中定义两个变量,用于保存两个数字
- 使用分支语句分两种情况对两个数字的大小关系进行处理
- 在main()方法中调用定义好的方法
- 代码:
public class MethodTest {
public static void main(String[] args) {
//在main()方法中调用定义好的方法
getMax();
}
//定义一个方法,用于打印两个数字中的较大数,例如getMax()
public static void getMax() {
//方法中定义两个变量,用于保存两个数字
int a = 10;
int b = 20;
//使用分支语句分两种情况对两个数字的大小关系进行处理
if(a > b) {
System.out.println(a);
} else {
System.out.println(b);
}
}
}
3. 带参数方法定义和调用
3.1 带参数方法定义和调用
-
定义格式:
参数:由数据类型和变量名组成 - 数据类型 变量名
public static void 方法名 (参数1) { 方法体; } public static void 方法名 (参数1, 参数2, 参数3...) { 方法体; }
-
范例:
public static void isEvenNumber(int number){ ... } public static void getMax(int num1, int num2){ ... }
-
注意:
方法定义时,参数中的数据类型与变量名都不能缺少,缺少任意一个程序将报错 方法定义时,多个参数之间使用逗号( ,)分隔
-
-
调用格式:
方法名(参数); 方法名(参数1,参数2);
-
范例:
isEvenNumber(10); getMax(10,20);
- 方法调用时,参数的数量与类型必须与方法定义中的设置相匹配,否则程序将报错
3.2 形参和实参
- 形参:方法定义中的参数
等同于变量定义格式,例如:int number
- 实参:方法调用中的参数
等同于使用变量或常量,例如: 10 number
4. 带返回值方法的定义和调用
4.1 带返回值方法定义和调用
-
定义格式
public static 数据类型 方法名 ( 参数 ) { return 数据 ; }
-
范例
public static boolean isEvenNumber( int number ) { return true ; } public static int getMax( int a, int b ) { return 100 ; }
- 注意:
- 方法定义时return后面的返回值与方法定义上的数据类型要匹配,否则程序将报错
- 注意:
-
调用格式
方法名 ( 参数 ) ; 数据类型 变量名 = 方法名 ( 参数 ) ;
-
范例
isEvenNumber ( 5 ) ; boolean flag = isEvenNumber ( 5 );
- 注意:
- 方法的返回值通常会使用变量接收,否则该返回值将无意义
- 注意:
5. 方法的注意事项
5.1 方法的注意事项
-
方法不能嵌套定义
-
示例代码:
public class MethodDemo { public static void main(String[] args) { } public static void methodOne() { public static void methodTwo() { // 这里会引发编译错误!!! } } }
-
-
void表示无返回值,可以省略return,也可以单独的书写return,后面不加数据
-
示例代码:
public class MethodDemo { public static void main(String[] args) { } public static void methodTwo() { //return 100; 编译错误,因为没有具体返回值类型 return; //System.out.println(100); return语句后面不能跟数据或代码 } }
-
5.2 方法的通用格式
-
格式:
public static 返回值类型 方法名(参数) { 方法体; return 数据 ; }
-
解释:
-
public static 修饰符,目前先记住这个格式
返回值类型 方法操作完毕之后返回的数据的数据类型
如果方法操作完毕,没有数据返回,这里写void,而且方法体中一般不写return
方法名 调用方法时候使用的标识
参数 由数据类型和变量名组成,多个参数之间用逗号隔开
方法体 完成功能的代码块
return 如果方法操作完毕,有数据返回,用于把数据返回给调用者
-
-
定义方法时,要做到两个明确
- 明确返回值类型:主要是明确方法操作完毕之后是否有数据返回,如果没有,写void;如果有,写对应的数据类型
- 明确参数:主要是明确参数的类型和数量
-
调用方法时的注意:
- void类型的方法,直接调用即可
- 非void类型的方法,推荐用变量接收调用
6. 方法重载
6.1 方法重载
-
方法重载概念
方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载
- 多个方法在同一个类中
- 多个方法具有相同的方法名
- 多个方法的参数不相同,类型不同或者数量不同
-
注意:
- 重载仅对应方法的定义,与方法的调用无关,调用方式参照标准格式
- 重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,换句话说不能通过返回值来判定两个方法是否相互构成重载
-
正确范例:
public class MethodDemo { public static void fn(int a) { //方法体 } public static int fn(double a) { //方法体 } } public class MethodDemo { public static float fn(int a) { //方法体 } public static int fn(int a , int b) { //方法体 } }
-
错误范例:
public class MethodDemo { public static void fn(int a) { //方法体 } public static int fn(int a) { /*错误原因:重载与返回值无关*/ //方法体 } } public class MethodDemo01 { public static void fn(int a) { //方法体 } } public class MethodDemo02 { public static int fn(double a) { /*错误原因:这是两个类的两个fn方法*/ //方法体 } }
练习
定义一个方法,该方法能够找出两个小数中的较小值并返回。在主方法中调用方法进行测试。
public class getDoubleMin {
public static double getMin(double num1,double num2){
if(num1<num2){
return num1;
}
else{
return num2;
}
}
public static void main(String[] args) {
double result = getMin(1.1,2.1);
System.out.println(result);
}
}
定义一个方法,该方法能够找出三个整数中的最大值并返回。在主方法中调用方法测试执行。
public class getMax {
public static void main(String[] args) {
int result = getMax(1,2,3);
System.out.println(result);
}
public static int getMax(int a,int b,int c){
int bigger = a>b?a:b;
int biggest = bigger>c?bigger:c;
return biggest;
}
}
在主方法中通过键盘录入三个整数。定义一个方法,方法接收三个整数变量,在方法中从大到小依次打印三个变量。
import java.util.Scanner;
public class threeIntArrange {
public static void main(String[] args) {
System.out.println("请输入第一个整数:");
int num1 = getNum();
System.out.println("请输入第二个整数:");
int num2 = getNum();
System.out.println("请输入第三个整数:");
int num3 = getNum();
arrage(num1,num2,num3);
}
public static int getNum(){
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
return num;
}
public static void arrage(int a, int b,int c){
int bigger = a>b?a:b;
int biggest = bigger>c?bigger:c;
int sum = a+b+c;
System.out.println("从大到小的顺序是:"+biggest+" "+bigger+" "+(sum-bigger-biggest));
}
}
数字是有绝对值的,负数的绝对值是它本身取反,非负数的绝对值是它本身。请定义一个方法,方法能够得到小数类型数字的绝对值并返回。请定义方法并测试。
public class getAbosolute {
public static void main(String[] args) {
double num1 = 1.1;
double result1 = getAbo(num1);
System.out.println(result1);
double num2 = -1.2;
double result2 = getAbo(num2);
System.out.println(result2);
}
public static double getAbo(double num){
double result;
if(num>=0) {
result = num;
}else{
result = -num;
}
return result;
}
}
键盘录入一个正整数
定义一个方法,该方法的功能是计算该数字是几位数字,并将位数返回
在main方法中打印该数字是几位数
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个正整数");
int number = sc.nextInt();
int result = getNumber(number);
System.out.println(number+"是"+result+"位数");
}
public static int getNumber(int num){
int count=0;
if(num>0){
while (true){
int ge = num%10;
num = num/10;
count++;
if(num==0){
break;
}
}
}else{
System.out.println("输入参数不合法");
}
return count;
}
}
需求:
定义一个方法equals(int[] arr1,int[] arr2).
功能:
比较两个数组是否相等(长度和内容均相等则认为两个数组是相同的)
public class Test3 {
public static void main(String[] args) {
int[] arr1 = {1,2,3,4};
int[] arr2 = {1,2,3,4};
int[] arr3 = {1,2,3};
int[] arr4 = {1,2,3,5};
boolean result1 = equals(arr1,arr2);
boolean result2 = equals(arr3,arr2);
boolean result3 = equals(arr4,arr2);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
}
public static boolean equals(int[] arr1,int[]arr2){
boolean flag = true;
//只有全部条件满足,flag才能被赋值为true
if(arr1.length==arr2.length){
for (int i = 0; i < arr1.length; i++) {
if(arr1[i]!=arr2[i]){
//一旦存在一个不相等,就返回false
flag = false;
break;
}
}
}else{
flag = false;
}
return flag;
}
}
需求:
定义一个方法fill(int[] arr,int value)
功能:
将数组arr中的所有元素的值改为value
public class Tesr2 {
public static void main(String[] args) {
//数组字面量无法直接传入change方法,需要先创建再传入
int[] arr1 = {1,2,3};
int[] arr = change(arr1,0);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
public static int[] change(int[] arr,int value){
for (int i = 0; i < arr.length; i++) {
arr[i]=value;
}
//返回的是int[]类型
return arr;
}
}
需求:
定义一个方法fill(int[] arr,int fromIndex,int toIndex,int value)
功能:
将数组arr中的元素从索引fromIndex开始到toIndex(不包含toIndex)对应的值改为value
public class Test4 {
public static void main(String[] args) {
int[] arr1 = {1,2,3,4,5,6,7};
int[] arr = fill(arr1,1,10,0);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
public static int[] fill(int[] arr,int fromIndex,int toIndex,int value){
if(fromIndex>=0&&toIndex<arr.length) {
for (int i = fromIndex; i < toIndex; i++) {
arr[i] = value;
}
}else{
System.out.println("索引超出范围,无法完成转换");
}
return arr;
}
}
需求:
定义一个方法copyOf(int[] arr, int newLength)
功能:
将数组arr中的newLength个元素拷贝到新数组中,并将新数组返回,从索引为0开始
public class Test5 {
public static void main(String[] args) {
int[] testArr = {1,2,3,4,5};
int[] arr1 = copyOf(testArr,3);
int[] arr2 = copyOf(testArr,7);
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i]+" ");
}
System.out.println();
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i]+" ");
}
}
public static int[] copyOf(int[] arr, int newLength){
int[] newArr;
if(newLength<=arr.length){
newArr = new int[newLength];
for (int i = 0; i < newLength; i++) {
newArr[i] = arr[i];
}
}else{
newArr = new int[arr.length];
System.out.println("需要拷贝的元素个数超出索引范围,现在将数组全部拷贝到新数组");
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
}
return newArr;
}
}
一个大V直播抽奖,奖品是现金红包,分别有{2,588,888,1000,10000}五个奖金。请使用代码模拟抽奖,打印出每个奖项,奖项的出现顺序要随机且不重复。
import java.util.Random;
public class Test6 {
public static void main(String[] args) {
int[] arr = {2,588,888,1000,10000};
int[] newArr = diasolate(arr);
for (int i = 0; i < newArr.length; i++) {
System.out.println(newArr[i]+"元的奖金被抽出");
}
}
//设计一个方法将抽奖池打乱
public static int[] diasolate(int[] arr){
Random r =new Random();
int tep = 0;
for (int i = 0; i < arr.length; i++) {
int index = r.nextInt(arr.length-1);
tep = arr[i];
arr[i] = arr[index];
arr[index] = tep;
}
return arr;
}
}
技巧
- 查看方法参数的快捷键:Ctrl + P
- 修改多个同名变量:Shift+F6
- 快速将一段代码抽取成一个方法:Ctrl+Alt+M