今天咱们讲讲Java中的函数也就是方法!!!
对了!给大家推荐一个刷题学习、面试神器——牛客网
里面有非常多的题库,跟面试经验~非常的良心!!
方法就是一个代码片段. 类似于 C 语言中的 “函数”。方法可以把它理解为一个功能,这个功能是可以重复使用的。
方法存在的意义:
目前来说写任何方法的时候都写成:
pubiic static 返回值 返回名称(形式参数列表){
函数体/方法体
}
代码举例:实现一个方法实现两个整数相加
public class Test {
public static void main(String[] args) {
int a = 10;
int b= 20;
int ret = add(a,b);
System.out.println("ret = " + ret);
}
public static int add (int x,int y) {
return x + y;
}
}
函数返回值的链式调用:
还是按照上面的例子给大家举例一下:
public class Test {
public static void main(String[] args) {
int a = 10;
int b= 20;
System.out.println("ret = " + add(a,b)*2);
}
public static int add (int x,int y) {
return x + y;
}
}
在Java里面没有函数声明,不管你的函数在main函数的上面还是在下面,都可以调用
注意事项
1 . public 和 static 两个关键字在此处具有特定含义, 我们暂时不讨论, 后面会详细介绍.
2. 方法定义时, 参数可以没有. 每个参数要指定类型
3. 方法定义时, 返回值也可以没有, 如果没有返回值, 则返回值类型应写成 void
4. 方法定义时的参数称为 “形参”, 方法调用时的参数称为 “实参”.
5. 方法的定义必须在类之中, 代码书写在调用位置的上方或者下方均可.
6. Java 中没有 “函数声明” 这样的概念.
函数开辟内存空间叫做函数栈帧,每个函数调用的时候都会开辟栈帧,属于这个函数的内存
函数在内存空间是如何调用的:
举例:用函数的方法求n的阶乘之和
//用函数的方法求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.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 方法.
这样的写法是对的(例如 Go 语言就是这么做的), 但是 Java 认为 addInt 这样的名字不友好, 不如直接就叫 add
方法名相同
方法的参数不同(参数个数或者参数类型)
方法的返回值类型不影响重载.
当两个方法的名字相同, 参数也相同, 但是返回值不同的时候, 不构成重载.
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();
}
//求水仙花数
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);
}
//调整数组顺序使得奇数位于偶数之前,调整之后,不关心大小顺序
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.用函数求三个数的最大值
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));
}
}
一个方法在执行过程中调用自身, 就称为 “递归”.递归相当于数学上的 “数学归纳法”, 有一个起始条件, 然后有一个递推公式.
例如, 我们求 N!
起始条件: N = 1 的时候, N! 为 1. 这个起始条件相当于递归的结束条件.
递归公式: 求 N! , 直接不好求, 可以把问题转换成 N! => N * (N-1)!
代码示例:
public static void main(String[] args) {
int n = 5;
int ret = factor(n);
System.out.println("ret = " + ret);
}
public static int factor(int n) {
if (n == 1) {
return 1;
}
return n * factor(n - 1); // factor 调用函数自身
}
递归过程咱们在C语言中已经讲过,想要学习递归的同学可以去看一看,咱们在Java中就不细讲了!
知识讲解: c语言基础篇:函数
练习:c语言:深度学习递归