一、方法的基本用法
1.1 什么是方法(method)
方法就是一个代码片段. 类似于 C 语言中的 "函数"。方法可以把它理解为一个功能,这个功能是可以重复使用的。
1.2 方法定义语法
目前来说写任何方法的时候都写成:
pubiic static 返回值 返回名称(形式参数列表){
函数体/方法体
}
代码举例:求1-n的和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * 求我们1-n的和 * 函数名字必须采用小驼峰 * @param n 输入一个数字 */ public static int sumAdd( int n){ int sum = 0 ; for ( int i = 1 ;i <= 10 ;i++){ sum += i; } return sum; }
public static void main(String[] args) { int ret = sumAdd( 10 ); //方法的调用 } |
画图讲解:
函数返回值的链式调用:
还是按照上面的例子给大家举例一下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * 求我们1-n的和 * 函数名字必须采用小驼峰 * @param n 输入一个数字 */ public static int sumAdd( int n){ int sum = 0 ; for ( int i = 1 ;i <= 10 ;i++){ sum += i; } return sum; }
public static void main(String[] args) { int ret = sumAdd( 10 ); //方法的调用 System.out.println(sumAdd( 10 )* 2 ); //函数的返回值,支持链式调用 } |
画图讲解:
在Java里面没有函数声明,不管你的函数在main函数的上面还是在下面,都可以调用
1.3方法的开辟
函数开辟内存空间叫做函数栈帧,每个函数调用的时候都会开辟栈帧,属于这个函数的内存
函数在内存空间是如何调用的:
举例:用函数的方法求n的阶乘之和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
用函数的方法求n的阶乘之和 public static int Fac( int n) { int sum = 1 ; for ( int i = 1 ; i <= n; i++) { sum = sum * i; } return sum; } /** * 求n的阶乘之和 * @param */ public static int facSum( int n){ int ret = 0 ; for ( int i = 1 ;i <= n; i++){ ret = ret + Fac(i); } return ret; }
public static void main(String[] args) {
System.out.println(facSum( 5 )); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
1 .Java无法通过传地址的方式交换两个值的变量,后续会讲怎么做 public class TestDemo { public static void swap( int a, int b){ //交换两个变量的值 int tmp = a; a = b; b = tmp; }
public static void main(String[] args) { int a = 10 ; int b = 10 ; System.out.println( "交换实参前:" +a+ " " +b); swap(&a,b); //Java是做不到取地址的,如果想要写一个函数交换两个数的的值,只能把a和b的值放到堆上 System.out.println( "交换实参后:" +a+ " " +b);
} } |
二、方法的重载
有些时候我们需要用一个函数同时兼容多种参数的情况, 我们就可以使用到方法重载
画图讲解:
方法的名字都叫 add. 但是有的 add 是计算 int 相加, 有的是 double 相加; 有的计算两个数字相加, 有的是计算三个数 字相加. 同一个方法名字, 提供不同版本的实现, 称为 方法重载
三、方法的使用
例题:用函数方式编写代码模拟三次登录场景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public static void login(){ int count = 3 ; Scanner scanner = new Scanner(System.in); while (count != 0 ){ System.out.println( "请输入你的密码" ); String password = scanner.nextLine(); if (password.equals( "123456" )){ //equals的返回值是true或者false System.out.println( "登录成功了" ); break ; } else { count--; System.out.println( "你输错密码了,你还有" +count+ "次机会" );
} }
}
public static void main(String[] args) { login();
} |
例题:函数求水仙花数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
求水仙花数 public static void findNum( int n){
for ( int i = 1 ; i <=n; i++) { int count = 0 ; //数字的位数 int tmp = i; while (tmp != 0 ){ count++; tmp = tmp/ 10 ; } tmp = i; int sum = 0 ; while (tmp != 0 ){ sum += Math.pow(tmp% 10 ,count); tmp/= 10 ; } if (sum == i){ System.out.println(i); }
}
}
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); findNum(n);
} |
例题:求奇数位于偶数之前
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
调整数组顺序使得奇数位于偶数之前,调整之后,不关心大小顺序 public class TestDemo {
public static void main(String[] args) { int []arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }; int left = 0 ; int right = arr.length- 1 ; while (left < right){ while (left < right && arr[left] % 2 != 0 ){ left++; } while (left < right && arr[right] % 2 == 0 ){ right--; } int tmp = arr[left]; arr[left] = arr[right]; arr[right] = tmp; } for ( int i = 0 ; i <arr.length ; i++) { System.out.print(arr[i]+ " " );
}
}
} |
例题: 函数求三个数的最大值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
1 .用函数求三个数的最大值 public class TestDemo {
public static int Max( int a, int b){ return a > b? a : b;
} public static int Max1( int a, int b, int c){ return Max(Max(a,b),c); }
public static void main(String[] args) { System.out.println(Max1( 4 , 6 , 8 ));
}
} |
总结:
本文简单介绍了什么是方法、方法的重载、方法如何使用。通过简单例题的方式加深对方法的印象。上述就是今天的内容,有任何疑问的话可以随时私信我,文章哪里出现了问题我都会积极改正,也希望大家能更快的掌握自己想要的知识,让我们一起加油!!!!!
我与你同在。
到此这篇关于Java 精炼解读方法的定义与使用的文章就介绍到这了,更多相关Java 方法内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/m0_64397675/article/details/123158093
查看更多关于Java 精炼解读方法的定义与使用的详细内容...