Detailed basis of the Java method

The method of property

  In the Java language, the method is equivalent to the function of the C language, but it also has the traditional function of a clear difference: the structure of the language, functions are first-class citizens, the entire program is one function of the composition; but in object-oriented languages, classes are first-class citizens, the entire program is composed of a number of classes. Therefore, in Java, the process can not exist, it can belong to the class or object. Therefore, if we want to define a method must be defined in the class. It is noteworthy that, if this method adds the modifier static (this modifier will be described in detail later), this method belongs to the class, otherwise, this method belongs to an instance of this class.

  We call the method way for: class method | | instance method. But when a method call our approach the same class in a class in, if this method is a common method, we need to use this to call; if this method is static methods (static modification), we need to use the class name to call.

 

Format description of the method

(1) Method format is defined as:

Modifier return type method name (parameter type parameter name 1, parameter type parameter name 2 ...) {
  method body statement;
  return return value;
}

Modifiers: Currently on the use of public static. We'll explain in detail later in other modifiers.
Return Value Type: Results of that data type. When this method does not return value, the return value of type void; When this method returns a value, the return value of type is determined (a return value if any, must return) in accordance with the returned data.

Method name: conform to the naming rules.

Parameters: actual parameters: that is actually involved in operations. Formal parameters; the method is defined for receiving the actual parameter.

Parameter type: it is the data type of the parameter
Parameter name: variable name is the
method body statements: completion code is functional.
return: the end of the process.
Returns: is the result of the function, the return gives the caller.

 

Method of parameter passing mechanism

The method is divided into two parameters: shape parameters and arguments. Representative of the parameter defined method, the parameters for receiving the actual parameter. Arguments on behalf of the parameters involved in the actual operation.

Java method parameters are passed are passed by value realization of the so-called value passed is a copy of the actual parameter values ​​are passed in the method, and the parameter itself will not be affected. Examples are as follows:

// a, b an example of the exchange value, but this value is just passed, a and b does not become 
public  class Fun {
     public  static  void main (String [] args) {
         int a =. 3, b =. 4 ; 
        fun1 ( a, B);                                // argument 
        System.out.println (a + "+++++" + B);   
    } 
        // methods in a class are prefixed with public static 
        public  static  void fun1 ( int a, int B ) {     // parameter 
        int TEMP; 
        TEMP = A; 
        A = B; 
        B =TEMP; 
        System.out.println (A + "========" + B); 
    } 
} 


// output is: 
4 ========. 3 
. 3 4 +++++
       

 

Number of variable process parameter

It allows the number of variable parameter indicates the number of parameter uncertainty specified as a method. If the method is defined in the parameter of the type plus the last three dots (...) indicates that the parameter can accept multiple values, a plurality of parameter values ​​are passed as an array. Examples are as follows:

public class fun {
    public static void main(String[] args) {
        fun1(4,"abc","dsa","bfd");
    }
    public static void fun1(int a,String... books){ //这里面的String... books相当于String[] books
        for(String book:books){
            System.out.println(book);
        }
        System.out.println(a);
    }
}

However, the number of variable parameter of this approach requires only variable parameter is the number of the last position parameter list, i.e., a process can have a maximum of a variable number of parameter.

 

Method overloading

  During the execution of the Java method, the overloaded concept is very important. The method defined in Java allows a plurality of the same name in the same class, as long as the parameter list can be different. If two or more include a method of the same name in the same class, but different parameter list (regardless of the type of return value), our method becomes overloaded. The so-called heavy duty is to ask the same two different: the same as the name of a class method, different parameter list. For the rest of the methods (return type, modifier, etc.) and reload nothing. It comprises different parameter list: A: number of different parameters. B: Different types of parameters. C: sequence of the different parameters (operator overloading, but not in development)

The following example demonstrates the reload class method:

public class fun {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        String c = "abc";
        fun1(a);
        fun1(a,b);
        fun1(a,c);
    }
    public static void fun1(int a){
        System.out.println(a);
    }
    public static void fun1(int a,int b){
        System.out.println(a+"==="+b);
    }
    public  static  void fun1 ( int A, B String) { 
        System.out.println (A + "===" + B); 
    } 
} 

// end result 
. 1 
. 1 === 2 
. 1 ABC ===

 

Guess you like

Origin www.cnblogs.com/liuzengzhi/p/11519388.html