Dabai became the fifteenth day of Java software siege lion (Java method overload mechanism overload, method recursion)

Method overload (overload)

  • Although the functions are different, when the functions are "similar", the method overload mechanism can make similar methods as if they are using one method. [Java supports this mechanism but some languages ​​do not, such as JavaScript]
  • When the functions are similar, the method names can be the same.

1. When should I consider using method overloading?

  • When the functions are similar, try to make the method names the same [but when the functions are different/not similar, make the method names as different as possible]

2. What conditions constitute method overloading after being met

  • In the same class
  • Same method name
  • The parameter list is different:
    1. The quantity is different: public static void m1(){} 与 public static void m1(int a){}
    2. The order is different: public static void m2(int a,double b){} 与 public static void m2(double a,int b){}
    3. The type is different:public static void m3(int x){} 与 public static void m3(double x){}
	方法重复,编译错误。
	public static void m4(int a,int b){
    
    }
    public static void m4(int b,int a){
    
    }

3. Method overloading has nothing to do with what, and nothing

  • Method overloading is related to method name + parameter list
  • Method overloading has nothing to do with return value type
  • Method overloading has nothing to do with modifier list

Method recursion

  • What is recursion?
    That is: the method itself calls itself.
	a(){
    
    
		a();
	}
  • Recursion consumes a lot of stack memory. Try not to use recursive algorithms when they are not in use.
  • Such an error occurs when the following program is running [not an exception, but an error error]:
    java.lang.StackOverflowError
    Stack memory overflow error The
    error cannot be recovered later, there is only one result. That is, the JVM stops working.
  • Recursion must have an end condition. Without an end condition, a stack memory overflow error will occur.
  • Even if the recursion has an end condition, even if the end condition is correct, a stack memory overflow error may occur because the recursion is too deep.

note:

  • Recursion can not be used as much as possible.
  • But in some cases the realization of this function must rely on recursion.

example

递归计算前n个数的和:
public class RecursionTest01{
    
    
	publci static void main(String[] args){
    
    
		int n=4;
		int reValue=sum(n);
		System.out.println(reValue);
	}
	
	public static int sum(int n){
    
    
		if(n==1){
    
    
			return 1;
		}
		return n+sum(n-1);	
	}
}
递归计算n的阶乘:
public class RecursionTest02{
    
    
	publci static void main(String[] args){
    
    
		int n=5;
		int reValue=method(n);
		System.out.println(reValue);
	}
	public static int method(int n){
    
    
		if(n==1){
    
    
			return 1;
		}
		return n*method(n-1);
	}
}

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/112849157