方法的重载以及方法的递归

方法重载

重载要解决的问题

class Test{
	public static void main(String[] args){
		int a = 10;
		int b = 20;
		int ret = add(a,b);
		System.out.println("ret = " + ret);

		double a2 = 10.5;
		double b2 = 20.5;
		double ret2 = add(a2, b2);
		System.out.println("ret2 = " + ret2);
	}

	public static int add(int x, int y){
		return x + y;
	}
}

//编译出错
Test.java:13:错误:不兼容的类型:从double转换到int可能会有损失
				double ret2 = add(a2,b2);

由于参数类型不匹配,所以不能直接使用现有的add方法。

class Test{
	public static void main(String[] args){
		int a = 10;
		int b = 20;
		int ret = addInt(a, b);
		System.out,println("ret = " + ret);

		double a2 = 10.5;
		double b2 = 20.5;
		double ret2 = addDouble(a2, b2);
		System.out.println("ret2 = " + ret2);
	}
	
	public static int addInt(int x, int y){
		return x + y;
	}
	
	public static double addDouble(double x, double y){
		return x + y;
	}
}

使用重载

class Test {
	public static void main(String[] args) {
		int a = 10;
		int b = 20;
		int ret = add(a, b);
		System.out.println("ret = " + ret);
		double a2 = 10.5;
		double b2 = 20.5;
		double ret2 = add(a2, b2);
		System.out.println("ret2 = " + ret2);
		double a3 = 10.5;
		double b3 = 10.5;
		double c3 = 20.5;
		double ret3 = add(a3, b3, c3);
		System.out.println("ret3 = " + ret3);
	}
	public static int add(int x, int y) {
		return x + y;
	}
	public static double add(double x, double y) {
		return x + y;
	}
	public static double add(double x, double y, double z) {
		return x + y + z;
	}
}

方法的名字都叫 add,但是有的 add 是计算 int 相加,有的计算两个 double 数字相加,有的是计算三个数字相加。
同一个方法名字,提供不同版本的实现,称为重载;

重载的规则

针对同一个类:
方法名相同;
方法的参数不同(参数个数或者参数类型);
方法的返回值类型不影响重载。

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 static double add(int x, int y) {
		return x + y;
	}
}
// 编译出错
Test.java:13: 错误: 已在类 Test中定义了方法 add(int,int)
public static double add(int x, int y) {
					  ^
1 个错误

当两个方法的名字相同,参数也相同,但是返回值不同的时候,不构成重载。

方法递归

递归的概念

一个方法在执行过程中调用自身,就称为“递归”。
递归求N的阶乘:

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 调用函数自身
}
// 执行结果
ret = 120

递归执行过程分析

递归的程序的执行过程不太容易理解,要想理解清楚递归,必须先理解清楚“方法的执行过程”,尤其是“方法执行结束之后,回到调用位置继续往下执行”。

public static void main(String[] args) {
	int n = 5;
	int ret = factor(n);
	System.out.println("ret = " + ret);
}
public static int factor(int n) {
	System.out.println("函数开始, n = " + n);
	if (n == 1) {
		System.out.println("函数结束, n = 1 ret = 1");
		return 1;
	}
	int ret = n * factor(n - 1);
	System.out.println("函数结束, n = " + n + " ret = " + ret);
	return ret;
}
// 执行结果
函数开始, n = 5
函数开始, n = 4
函数开始, n = 3
函数开始, n = 2
函数开始, n = 1
函数结束, n = 1 ret = 1
函数结束, n = 2 ret = 2
函数结束, n = 3 ret = 6
函数结束, n = 4 ret = 24
函数结束, n = 5 ret = 120
ret = 120

执行过程图
在这里插入图片描述

递归小结

递归是一种重要的编程解决问题的方式
有些问题天然就是使用递归方式定义的(例如斐波那契数列,二叉树等),此时使用递归来解就很容易。
有些问题使用递归和使用非递归(循环)都可以解决,那么此时更推荐使用循环,相比于递归,非递归程序更加高效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值