Definition of the method - Java foundation

1. Why do you need a method?

  Method (also known as function) is the period of a particular function block. Methods to improve the reusability and readability.

  For example, with the method, we can put a piece of code you want to reuse extracted, then you need to perform in every place to call to this code.

2. The method of format

grammar:

Access modifier [] Other modifiers, such as static return type method name (parameter 1 Parameter Type 1, Type 2 Parameter 2 ..... parameter) {// parameter list

    // method body

    return Return value;

}

3. The method of format specification

Modifiers: Currently on the use of public static. Follow-up supplement

Return Value Type: Results of that data type.

Method name: See-known name meaning, first letter lowercase, follow hump nomenclature. We call convenience.

The method involved in type argument

parameter:

  The actual parameters: that is actually involved in operations.

  In the form of parameters: the method is defined, for receiving the actual parameter.

Parameter type: it is the data type of the parameter

Parameter name: is the variable name.

Method body statement: is the complete code for the function.

note:

(1) If the current method does not require the use of parameter, the parameter list can be empty.

(2) arguments and parameters of the type to be compatible with each other, and: argument range less than or equal to the range parameter type.

 

In the call method, if the method we have defined parameters, it is necessary to pass at the same time calling method of this value, that is, to the current method of parameter assignment, and this value is called the actual parameter passed in, that is the argument.

Arguments: the value of the parameter passed

Parameter: receiving a pass over the argument value.

Argument name and parameter names may be the same or different.

Parameter is a variable, argument is a value, mass participation is to assign a value to an image.

The method returns the value of

retturn: Method end

Returns: is the result of the function, the return gives the caller.

(1) If the current process is not the type of the return value, i.e., a void return type, then the current write process may not return.

(2) return means that the end of a method, the return value may be returned to the caller to the caller calling the current method.

(3) return a return value returned only when the value should not return multiple values.

(4) a process may have a plurality of return, but only one, the determination needs to be performed.

6. The method of overloading

Method overloading: overloading method

You can create multiple methods within a class, they have the same name but with different parameters and different definitions;

Overload conditions can not return a value.

Such as:

public void method(int a){....}

public void method(char c){....}

Guess you like

Origin www.cnblogs.com/s1023/p/11145198.html