catalogue
3. Method without parameter and return value
3.4 method implementation and call demonstration
3.6 method execution flow diagram
4. Method with parameter and no return value
4.4 method implementation and call demonstration
4.6 method execution flow diagram
5. Method with return value without parameter
5.4 method implementation and call demonstration
5.6 method execution flow diagram
6. Methods with parameters and return values
6.4 method implementation and call demonstration
6.6 method execution flow diagram
[supplement: document note template]
1. Method introduction
During development, the function code is used many times. If the CV method is used to solve the problem, it will lead to
1. Code redundancy
2. Poor code reading
3. Poor code maintainability
Methods can be used to solve the above problems
2. Format
public static void main(String[] args) { } /* public static: Fixed format void: Method. void indicates that the current method has no return value. The return value of the method can be determined according to the analysis process of the method. main: Method name, see name to know meaning, verb object structure, small hump naming method. A good method can tell the user what the current method is doing through the method name. (Formal parameter list): Foreign data required for the current method to run. be similar to Ingredients corresponding to the recipe -- > formal parameters According to the actual content of recipe raw materials -- > actual parameters {}: Method body, encapsulated function code. Summary method format: public static Return value type method name (formal parameter list){ Method body; } public static returnType methodName(Parameters) { Method Body; } */
3. Method without parameter and return value
3.1 requirements
Show one
New year's Eve meal (steamed bass, stewed beef with sauce, roast lamb chops, potato ribs, boiled cabbage, eight treasure rice, mixed dishes)
3.2 method analysis
public static:
Fixed format
Return value type:
The current method has no return value, which is void.
void indicates no return value status.
The function of the current method is to display data and return no data. [be sure to distinguish between data display and data return.]
[data display] the chef cooked kung pao chicken and ate it himself
[data return] the chef made kung pao chicken and gave it to you
Method name:
Methods functional display, new year's Eve dinner, verb object structure, small hump naming method
showDinner
Formal parameter list:
The current method has no external data requirements and does not need parameters, but the corresponding format of parameters must have ()
3.3 method statement
public static void showDinner();
3.4 method implementation and call demonstration
/* No parameter no return value method definition operation */ class Demo2 { public static void main(String[] args) { /* [Note 3] call method The way is for the dog to roar where it needs to, roar [name] [Note 4] The biggest difference between calling a method and using a variable is the parentheses, which call a method Must have parentheses */ showDinner(); showDinner(); showDinner(); showDinner(); showDinner(); } /* [Note 1] The custom method is outside the main method, within class braces, and is the same level as the main method [Note 2] Be sure to complete the document comments */ /** * Show this year's new year's Eve dinner */ public static void showDinner() { System.out.println("family reunion dinner(Steamed Perch, Braised beef in sauce, baked lamp chop, Potato ribs, Boiled cabbage heart, eight treasure rice, mixed vegetables, dumplings)"); } }
3.5 precautions
1. Method is to declare the method first and complete the method before calling / using it
Method must be ready to be called for execution.
2. Method execution can only be executed through this call, which has nothing to do with the location and order of the method.
For example:
The menu order of the hotel has nothing to do with the order order of the user
The number of menu items in the hotel has nothing to do with the number of dishes ordered by the user
3. The execution process of the method is only related to the calling sequence.
For example:
The restaurant orders, boss. Let's have kung pao chicken first.
Boss, another shredded pork with fish sauce
Boss, another sauce elbow
The boss wants another dumpling
4. Each method must have corresponding document comments, which must be written!!!
3.6 method execution flow diagram
4. Method with parameter and no return value
4.1 requirements
Display the int type data provided by the user
4.2 method analysis
public static:
Fixed format
Return value type:
The current method has no return value and directly displays the data} void entered by the user
Method name:
See name and meaning, verb object structure, small hump naming method.
Display int type data
showIntValue
show shows the value data of int standard integer type
Display int type data
Formal parameter list:
The current method has external data requirements and provides a parameter list of corresponding data types
(int num)
int is the data type of the current parameter
num is the name corresponding to the parameter, which can be considered as the parameter name. At the same time, it can also be used directly inside the method
It can accept the parameter data passed in by the user during the call process. And store data for use during the operation of the method.
The current method provides an interface for the user to pass in data of type int
4.3 method statement
public static void showIntValue(int num);
4.4 method implementation and call demonstration
/* Demonstration of method with parameter and no return value */ class Demo3 { public static void main(String[] args) { /* Call method The current method has corresponding parameter requirements and needs to be given the corresponding type of actual data during the call process. The data type of the actual data must be consistent with the type required by the parameter */ showIntValue(15); showIntValue(100); showIntValue(20); // showIntValue(); /* Demo3.java:13: Error: cannot apply the method showIntValue in class Demo3 to the given type; showIntValue(); No, I didn't give the parameters ^ Expected: int Found: no parameters Reason: the length of actual parameter list and formal parameter list is different 1 Errors */ // showIntValue(10, 5, 20, 30, 50, 60); /* Demo3.java:24: Error: cannot apply the method showIntValue in class Demo3 to the given type; showIntValue(10, 5, 20, 30, 50, 60); There are too many parameters, No ^ Expected: int Found: int,int,int,int,int,int Reason: the length of actual parameter list and formal parameter list is different 1 Errors */ // showIntValue(50.5); /* Demo3.java:35: Error: cannot apply the method showIntValue in class Demo3 to the given type; showIntValue(50.5); Wrong parameter type, no ^ Expected: int Found: double Cause: parameter mismatch; Conversion from double to int may be lossy 1 Errors */ } /** * Displays the int type data provided by the current user * * @param num The int type data provided by the user is required for method execution presentation */ public static void showIntValue(int num) { // Display data provided by users System.out.println(num); } }
4.5 precautions
When calling a method with parameters, the actual parameters must be consistent with the parameter type, number and order declared by the method in the process of passing in the method.
4.6 method execution flow diagram
5. Method with return value without parameter
5.1 requirements
Call the method and return an integer of 5
5.2 method analysis
public static
Fixed format
Return value type:
The current method returns an integer 5 of type int
The return value of the method is of type int.
Method name
See name, know meaning, verb object structure, small hump naming method
giveMeFive
Formal parameter list
There is no parameter requirement for the current method. Even if there is no parameter method, there must be ()
5.3 method statement
public static int giveMeFive();
5.4 method implementation and call demonstration
/* Demonstration of method with return value without parameter */ class Demo4 { public static void main(String[] args) { int num = 0; // Before calling the method, show the data stored in num System.out.println("Before method call:" + num); /* giveMeFive() If there is a return value, it can be directly placed on the right side of the assignment number, and the method can be used to return data Assign a value to a variable */ num = giveMeFive(); // After the method is called, the data stored in num is displayed System.out.println("After method call:" + num); } /** * The current method returns an int type data 5 * * @return The return value of the current method is of type int, and the specific data is integer 5 */ public static int giveMeFive() { /* return keyword: 1. Return the data after return outside the method 2. A method terminates after running to return. return The subsequent data must be exactly the same as the return value type of the method declaration. Ensure [data type consistency] */ return 5; } }
5.5 precautions
1. The return value required by the method needs to be returned through return. It requires the return value type declared by the method. Return must provide the corresponding data as required. It cannot be missing or wrong type. The code must be completed in strict accordance with [data type consistency].
2. Code of the same level as return cannot be run after return. Is an invalid code. Unreachable Code unreachable code
5.6 method execution flow diagram
6. Methods with parameters and return values
6.1 requirements
Calculate the sum of two int type data provided by the user, and return the result through the return value
6.2 method analysis
public static
Fixed format
Return value type:
When two int data types are added, the return value is the int type, regardless of too much trouble
Method name:
See name, know meaning, verb object structure, small hump naming method
getSum
Get get Sum and
Formal parameter list:
The user is required to provide two int type parameters
(int num1, int num2);
In the case of a method with multiple parameters, the formal parameter list format
(data type 1, parameter variable 1, data type 2, parameter variable 2, data type 3, parameter variable 3)
A data type corresponds to a parameter. Different parameters are separated by commas
6.3 method statement
public static int getSum(int num1, int num2);
6.4 method implementation and call demonstration
/* Demonstration of methods with parameters and return values */ class Demo5 { public static void main(String[] args) { // Define an int type variable to store the return value data of the method int sum = 0; // Show data storage before method call System.out.println("Before calling the method:" + sum); /* Call the method. The parameters passed in by calling the method are integers 10 and 20 And assign the return value of the current method to the variable sum */ sum = getSum(10, 20); // After the method is called, the data storage is displayed System.out.println("After calling the method:" + sum); } /** * The user provides two int type data and returns the sum of the two int type data * * @param num1 The first int type data provided by the user * @param num2 The second int type data provided by the user * @return int Type data, sum of two integers */ public static int getSum(int num1, int num2) { return num1 + num2; } }
6.5 precautions
1. When calling a method, the actual parameters required to be provided must be completely consistent with the parameter type, number and order of the method declaration.
2. There are parameters and methods corresponding to return values. Document comments need to explain method functions, parameter information, and return values
6.6 method execution flow diagram
[supplement: document note template]
Documentation Comments [a key]: effect: Explain the use requirements and details of classes, variables and methods in the code, [instructions] stay Java Method document annotation requirements /** * Brief overview and function overview of current method operation. Cases and precautions can be added in the description process. * * @param Parameter name 1 refers to the interpretation and demand analysis of current parameters and function overview * @param Parameter name 2 refers to the interpretation and demand analysis of current parameters and function overview * @return Return value data information description and special case description * Other content * @throws Exception thrown * @Exception Exception declaration * @since Minimum version requirements * @author author * @date date */