Object-oriented programming (four)

Disclaimer: All my articles are the compilation of online teaching videos, including Mad God Talk, Shang Silicon Valley, Dark Horse Programmer, etc., used as reference materials, without any commercial use, please refer to the majority of netizens, please do not spray ,Thank you. (Note that due to the website, some code characters may have problems. It is recommended that when reading the code, it is best to look at the corresponding picture below)

1. Method overload (overload)

Concept: In the same class, more than one method with the same name is allowed, as long as their number of parameters or parameter types are different.
Features: It has nothing to do with the return value type, just look at the parameter list, and the parameter list must be different (number of parameters or parameter types). When calling, they are distinguished according to the method parameter list. Using the overload method can bring convenience to programming.
Example:
//Return the sum of two integers
int add(int x,int y) {
return x + y;
}
//Return the sum of three integers
int add(int x,int y,int z) {
return x + y + z;
}
//Return the sum of two decimals
double add(double x, double y) {
return x + y;
}
Exercise:
1) Write a program, define three overloaded methods and call them. The method is named mOL. Requirements: The three methods respectively receive an int parameter, two int parameters, and a string parameter. Perform squaring and output the result respectively, multiply and output the result, and output string information. In the main () method of the main class, the three methods are called separately with parameters.
2) Define three overloaded methods max(). The first method finds the maximum of two int values, the second method finds the maximum of two double values, and the third method finds three double values. And call the three methods respectively.

Two, variable number of formal parameters

Before JDK5.0: use array parameters to define methods, and pass in multiple variables of the same type,
such as: public static void test(int a,String[] books);
JDK5.0: use variable number of parameters to define methods , Pass in multiple variables of the same type,
such as: public static void test(int a,String ... books);
Description:
1. Declaration format: method name (parameter type name... parameter name)
2. Variable parameters : The number of parameters of the specified type in the method parameter part is variable, 0, 1, or more
3. The method of variable number of formal parameters and the method of the same name form an overload
4. Variable parameters The use of the method is consistent with the use of the array in the method parameter part
5. The parameter part of the method has deformable parameters, which need to be placed at the end of the formal parameter declaration.
6. In the formal parameter position of a method, only one variable number can be declared at most
Examples of formal parameters :
Object-oriented programming (four)
Object-oriented programming (four)

Third, the value transfer mechanism of method parameters

A method must be called by its class or object to make sense. If the method contains parameters, then the formal parameter is the parameter when the method is declared, and the actual parameter is the parameter value actually passed to the formal parameter when the method is called.
How to pass the actual parameter value of Java into a method:
In Java, there is only one way of passing method parameters, that is, value passing, which passes a copy (duplicate) of the actual parameter value into the method, and the parameter itself is not affected.
 Formal parameter is a basic data type: Pass the "data value" of the basic data type variable of the actual parameter to the formal parameter
 Formal parameter is the reference data type: Pass the "address value" of the actual parameter reference data type variable to the
basic data of the formal parameter Examples of type parameter passing:
public static void main(String[] args) {
int x = 5;
System.out.println("Before modification x = "+ x); // 5
// x is the actual parameter
change(x );
System.out.println("After modification x = "+ x);// 5
}

public static void change(int x) {
System.out.println("change: before modification x = "+ x);
x = 3;
System.out.println("change: after modification x =" + x);
}
Object-oriented programming (four)
Object-oriented programming (four)
The parameter passing of the reference data type is
Object-oriented programming (four)
defined as:
Object-oriented programming (four)
Object-oriented programming (four)
Exercise:
1. Read the program code and write the result
Object-oriented programming (four)
2. Read the program code and write the result
Object-oriented programming (four)

Four, recursion (recursion) method

Definition: A method body calls itself.
Explanation: Method recursion includes an implicit loop, which will repeatedly execute a certain piece of code, but this kind of repeated execution does not require loop control; recursion must recurse in a known direction, otherwise this recursion will become infinite recursion. Similar to an endless loop.
Example: Calculate the sum of all natural numbers between 1-100.
Object-oriented programming (four)
Exercises:
1. Please use Java to write the recursive factorial (n!) algorithm.
2. There is a sequence of numbers: f(0) = 1, f(1) = 4, f(n+2)=2 f(n+1) + f(n), where n is an integer greater than 0, Find the value of f(10).
3. Given a sequence of numbers: f(20) = 1, f(21) = 4, f(n+2) = 2
f(n+1)+f(n), where n is an integer greater than 0, find The value of f(10).
4. Enter a piece of data n and calculate the nth value of the Fibonacci sequence (Fibonacci sequence: 1 1 2 3 5 8 13 21 34 55... Law: a number equals the first two numbers Sum), requirement: Calculate the nth value of the Fibonacci sequence (Fibonacci) and print out the entire sequence.

Guess you like

Origin blog.51cto.com/12859164/2546180