Silicon Valley is still day05 Java method (method) and heavy (overload)

The method of the fifth day (method) and heavy (overload)

1. The special flow control statements

(1)break

break action: break or loop or switch statement; disruption from the break is the most recent cycle.
Break tagged: go along with the label would be finished, break which label on which interrupt the cycle.

(2)continue

continue action: When interrupt cycles, the cycle time of execution continues; interrupts is the nearest distance continue when cycles.
Continue tagged: go along with the label on the end, which continue the cycle when the cycles which label is interrupted.

【Exercise】

(1) break find all the prime numbers less than 100;
(2) receiving a n, so that should have 100 cycles of the cycle, the actual let loop n times;
(3) print the odd-numbered less than 100 (using Continue) (difficulty) ;
Continue (.

2. Methods

(1) Statement

Methods: The behavior of the class or object, capsulation function, in order to perform certain functions.

Precautions: The method can only be defined in a class, and other methods may not be nested !!!

Statement format:
modifier return type method name (a data type parameter 1, parameter data type 2 2, ...) {
statement block;
return value return;
}

Description:

Return Value Type: Data Type method of final results, if the method does not return void value on the use of
the method name: identifier
in the form of parameters: the method requires some data, specific data is not sure how much, but how much specific data no effect on the function
return value: the results of products of the process, and finally to the user if the method does not return a value, you can omit the final return.
the actual parameters: when the user is really using this method of data transfer
method = method signature (method using a manual API) + method body (code actually executed)

(2) call

The method is called only will be executed

Method call statement: the method name (argument 1, argument 2, ...);

After receiving the return value of the method call completes

Method call itself can be seen as a return value, so you can directly use the variable that receives the return value

Return Value Type = variable name method call;

【Exercise】

(1) TestMethod a defined class, method call a method in the main method main, rectangular printout 20 * 8;
(2) Printing a rectangular perimeter

3. Overload

In the same class, the same method name, a list of different parameters (different parameters), called reload.

Lists reflect the different parameters: number of different parameters, different types of parameters, different parameters order.

Note: the type of method overloading and returns the value regardless of the override function is generally similar to the method.

Overloaded benefits: ease of use caller only need to remember a method name.

4. Analysis of transmission parameters

-------- arguments "parameter
Note: The argument only to the argument that was passed is a copy of the argument, it does not affect the parameter.

5. unity, a chain transfer

For example: print a special rectangle: square

[Practice] to use to print squares and rectangles

6. call execution process analysis method

The method of execution is equivalent to a stack: last-out, the return value is stored in the temporary memory space.

7. recursive (method calls itself)

(1) infinite recursion -> stack overflow -> java.lang.StackOverloadError

[Practice] to print using a recursive factorial 5

(2) application scenario: a problem can be decomposed subproblems of the same type.

8. Analysis of double recursion

9. The cross-category method call

use"."

10. [work] to do a project (Bank Accounting)

Released six original articles · won praise 0 · Views 165

Guess you like

Origin blog.csdn.net/qq_41818801/article/details/103951361